Monday, February 18, 2013

Re: Custom Http404 like exception

Hey Serge,

Here is a brief description of how status code is set in request response cycle of Django.


get_response() method of django.core.handlers.base.BaseHandler handles Http404 and sets status code herehttps://github.com/django/django/blob/stable/1.4.x/django/core/handlers/base.py#L138-L155

django.core.handlers.base.BaseHandler has some code for setting status_code to 403 and 500 as well.

I want to make the same but with for a 401 code and a custom redirection according to the resource to be accessed.

There is really simple way to do this.

    
class HttpResponseUnauthorizedRequest(HttpResponse):
    status_code = 401

You can use this class similar to HttpResponse

    def protected_view(request):      ## handle authorization      ## if not authorized then return above response      return HttpResponseUnauthorizedRequest("Authorazation is required")

There are similar classes for different status code as well - https://github.com/django/django/blob/stable/1.4.x/django/http/__init__.py#L751-L770


So you have set status code, now what

process_response() method of django.middleware.common.CommonMiddleware checks if status code is 404 and sends an email to admins for broken urls - https://github.com/django/django/blob/stable/1.4.x/django/middleware/common.py#L94-L109

You can see status_code based processing of response in other middlewares as well, e.g. cache, http and csrf.

Finally request handlers set status code is response header - 

1. django.core.handlers.wsgi.WSGIHandler sets status code in response header - https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/wsgi.py#L245-L253

2. If you are using modpython then status_code in header is set by django.core.handlers.modpython.ModPythonHandler - https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/wsgi.py#L245-L253

This response is served by Webserver.

Sincerely,
Pankaj Singh


On Mon, Feb 18, 2013 at 6:15 PM, Serge G. Spaolonzi <serge@cobalys.com> wrote:
Hi,

How does the Http404 exception works internally?
I want to make the same but with for a 401 code and a custom
redirection according to the resource to be accessed.
I have thought about using a middleware to archive this but I am not
sure if it is standard way used by 'django.http.Http404'.

Thanks

--
Serge G. Spaolonzi
Cobalys Systems
http://www.cobalys.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment