~hruske Hruške, jabuke, jablane, čežane. » Blog Archive » On how Django handles your browser’s request
Home Contact Sitemap

Hruške, jabuke, jablane, čežane.

Ste se gdaj vprašali, zakaj Najboljšega soseda nikoli ni doma, ko pridete na obisk?

On how Django handles your browser’s request

Posted on September 16th, 2005 in kitchen sink |

Django is a quiet powerful framework for creating web services like news sites, blogs and similar. One of it’s greatest flaws is rare documentation available and few times rarer are the people that understand how that piece of code works.

A request comes to Apache which then forwards it (if properly configured) to mod-python, in the file django/core/handlers/modpython.py. ModPythonHandler, defined in that file, is a subclass of BaseHandler from django/core/handlers/base.py. In ModPythonHandler Django loads the settings, the database and loads the installed middleware. It then calls the get_response function, defined in base.py. It is responsible for returning the rendered page, which Django people call HttpResponse.

The order of actions in get_response function is key to understanding Django middleware. Firstly request middleware is applied. Middleware can only return HttpResponse, so if middleware returns anything, the HttpResponse is returned to browser. Then the url is resolved to view. This is where Django checks for a view fitting to the patterns defined in your urls.py files. When view is found (if it isn’t, the Http404 is raised and you get a well known ‘page not found’ error), the view middleware is applied to view. If view middleware does not return a HttpResponse, the view is called. The view must return reponse, though, so if it does not, an error gets raised.

This was the get_response, after this, the response middleware is applied and HttpResponse is passed back to Apache.

There are three types of middleware: request, view and response. If we take a look at django/middleware/cache.py, we see that applying middleware means calling a function named process_request, process_view or process_response. In the example for cache, there are process_request and process_response. When a request comes, if it’s lucky enough to have a cached version, that’s the one that will get returned. This happens in process_request. The process_response cache middleware is a way for the rendered page to get into cache.

I hope the clouds go away and the image clears one day … soon…

Comments are closed.

Komentarji so izklopljeni