In this post I'll going to explore what happens behind the scenes when we make a request for a resource (normally a template) and how Django sends the response.
All of the action happens in the django/core/handlers/base.py file.
1. On request server make server instance which is django.core.handlers.BaseHandler object. The server can be mod_python or wsgi type.
2. Import project's setting.py file and Django custom exception classes.
3. loads middleware classes by calling the load_middleware() of BaseHandler.
(ImproperlyConfigured exception is thrown from here)
4. server instance dispatches the request_signal and create the instance of HttpRequest. this request object may be of mod_python [ django.core.handlers.wsgi.ModPythonRequest ] or wsgi [ django.core.handlers.wsgi.WSGIRequest] depending upon the server type.
5. get_reponse(request) of BaseHandler is called. Its main purpose is to return HttpResponse object. This is most important method to for a request to get successful response or otherwise.
I have divided the working of get_reposne(request) in following two phases:
phase 1: request
a). Execute the process_request(reqest) of middleware by passing the request object.
b). If process_request() returns the None then resolves the url by calling resolve() on
django.core.urlresolvers.
c). If url resolution is successful then calls process_view( request, view_fuc, url_arg )
If url is not successful then returns the django.http.Http404.
process_view( request, view_fuc, url_arg ) either returns None or HttpResponse object.
If it returns None then Django will continue processing the request and will call appropriate view.
If it returns the HttpResponse object then django will not call view.
phase 2: response
When view() is called and it should either response or exception.
If response is response_object :
a).process_template_response(request, reponse ) is called by view() if response instance has render() associated with it, response parameter is of type TemplateResponse.
It is where you get the required response (html).
b).process_template_response(request, reponse ) is called by view() if response instance has render() associated with it, response parameter is of type HttpResponse or StramingHttpResponse.
If response is None:
c). process_excpetion(request, exception) is called which throws ValueError like:
So for every request(url) either you will get response (e.g. html) or exception.
All of the action happens in the django/core/handlers/base.py file.
1. On request server make server instance which is django.core.handlers.BaseHandler object. The server can be mod_python or wsgi type.
2. Import project's setting.py file and Django custom exception classes.
3. loads middleware classes by calling the load_middleware() of BaseHandler.
(ImproperlyConfigured exception is thrown from here)
4. server instance dispatches the request_signal and create the instance of HttpRequest. this request object may be of mod_python [ django.core.handlers.wsgi.ModPythonRequest ] or wsgi [ django.core.handlers.wsgi.WSGIRequest] depending upon the server type.
5. get_reponse(request) of BaseHandler is called. Its main purpose is to return HttpResponse object. This is most important method to for a request to get successful response or otherwise.
I have divided the working of get_reposne(request) in following two phases:
phase 1: request
a). Execute the process_request(reqest) of middleware by passing the request object.
b). If process_request() returns the None then resolves the url by calling resolve() on
django.core.urlresolvers.
c). If url resolution is successful then calls process_view( request, view_fuc, url_arg )
If url is not successful then returns the django.http.Http404.
process_view( request, view_fuc, url_arg ) either returns None or HttpResponse object.
If it returns None then Django will continue processing the request and will call appropriate view.
If it returns the HttpResponse object then django will not call view.
phase 2: response
When view() is called and it should either response or exception.
If response is response_object :
a).process_template_response(request, reponse ) is called by view() if response instance has render() associated with it, response parameter is of type TemplateResponse.
It is where you get the required response (html).
b).process_template_response(request, reponse ) is called by view() if response instance has render() associated with it, response parameter is of type HttpResponse or StramingHttpResponse.
If response is None:
c). process_excpetion(request, exception) is called which throws ValueError like:
raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
So for every request(url) either you will get response (e.g. html) or exception.
No comments:
Post a Comment