19

so I have been playing around with django for a bit and I really do like this framework. However, I would like to understand better how it actually works 'under the covers'.

Here is my current view of client-server-django world, which is very rough and will probably make your toenails curl (sorry)...

  1. The browser sends a Http request to the server.
  2. The server does its magic and dumps the request via the CGI to django (?)
  3. Some part of django (which?) receives the request and turns it into a django request object.
  4. The request object wanders on some nebulous paths through the middleware which does strange things with it.
  5. The request object finally ends up in some function (which?) which looks at the urls, takes the patterns out of urls.py and calls up a view function.
  6. The view functions do their magic (with models and templates as partners in vice) , this is probably where I have the strongest illusion of understanding (well, apart from the database abstraction magic, that is... ;)
  7. The view functions returns an HttpResponse object, I guess this is returned on some nebulous paths to the CGI.
  8. Webserver takes over again and sends the Http response to the client.

Ok, so what the heck is my question you ask? Well, how does this all work, really? I am not expecting that you spoon-feed me everything... I suspect that the answer will ultimately be to "read the source, luke", however, I would be grateful if

  1. You could clear up my grosses misconseptions
  2. tell me where to start? What I would like to do is grap a debugger and just walk through the process a couple of times, but I don't really know where to get started
  3. you could point me to any documents that explain this well... yes, I have heard of this google thing but haven't really found anything super-useful.

thanks a lot Paul

Paul
  • 7,836
  • 2
  • 41
  • 48

2 Answers2

13

Well, your first misconception is that CGI has anything to do with this. It doesn't, except very unlikely and little-used server configurations.

Mostly, Django interfaces with the server via WSGI, which is a Python specification for web servers to talk to web applications. You can see more information at the WSGI website.

Apart from that, the whole request/response cycle is very well described by James Bennett in his blog entry here. It's quite old, but little has changed in Django at that level since it was written.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hey, thanks for the two articles, very helpful! I am currently looking at the "James Bennett" one... So is WSGI a kind of CGI then (well, I guess I'll find out reading ;) ) Thanks! – Paul Nov 24 '10 at 15:42
10

Watch James Bennett's Django in Depth tutorial from Pycon 2015.

From the Pycon website, here's the abstract of James' talk:

Most books, tutorials and other documentation for Django take a high-level approach to its components and APIs, and so barely scratch the surface of the framework. In this tutorial, however, we'll take a detailed look under the hood, covering everything from the guts of the ORM to the innards of the template system to how the admin interface really works.

Whether you're the newest of newbies or the most seasoned of application developers, you'll come away with a deeper knowledge of Django, and a plethora of new tips and tricks you can use in your own applications.

Community
  • 1
  • 1
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • 3
    There currently exists no other seminal as extensive and deep as DiD. Bennett ran a nail into every feature of Django. Here are the slides (whopping 1025 of them) from the presentation http://www.slideshare.net/ubernostrum/django-in-depth. – Filip Dupanović Nov 24 '10 at 16:04
  • Thanks for the link to the slides KRON! It scared me a bit when I read 1025, but it seems that's counting all the overlays, so I guess I can go through them in a night ;) – Paul Nov 24 '10 at 16:53