How do I return a 401 Unauthorized in Django?


Question

Instead of doing this:

res = HttpResponse("Unauthorized")
res.status_code = 401
return res

Is there a way to do it without typing it every time?

1
40
12/5/2010 1:15:28 AM

I know this is an old one, but it's the top Google result for "django 401", so I thought I'd point this out...

Assuming you've already imported django.http.HttpResponse, you can do it in a single line:

return HttpResponse('Unauthorized', status=401)

The 'Unauthorized' string is optional. Easy.

116
5/18/2012 8:20:56 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon