Friday, 1 May 2015

Django's Component Architecture

In this post we will have a rudimentary discussion about how a URL hits a server, how the request is served and a response is returned to the client. The following diagram depicts the entire life cycle of a request and response in context of Django Components.

A high-level overview of Django's Component Architecture
Credits: This diagram is taken from book Python Web Development with Django by Jeff Forcier, Paul Bissex and Wesley Chun.

When a user (browser) hits a URL, e.g. http://192.168.10.1:8080/library/books/, then over the Internet it finds the machine with IP address 192.168.10.1 and hits the service running on port 8080. This service is the Server (which we run using manage.py for non-production environments). The server picks up the request and forwards it to the Django framework.

Inside Django, the request is accepted by Request Middleware. This middleware enriches the Response object and forwards the request along with URL /library/books/ to the file specified by ROOT_URLCONF variable in settings.py (which is a urls.py file). 

In urls.py file there is a list of url patterns and the corresponding views.The entry in the list matching /library/books is identified, it's associated view is picked and this information along with request object is forwarded to View Middleware.

The View Middleware enriches response object and delivers the request (in **args parameter) along with **kwargs to the appropriate function or class-based view (generic, sem-generic or custom) in views.py. The **args contain request object as parameter (and the unnamed parameter groups extracted from URL) and **kwargs contain the parameters as key value pairs extracted from URL e.g. library/books/(?P<book_id>\d+)$, then value of book_id will be passed via **kwargs as argument.

The view can communicate with database via model -> ORM -> Python DB-API || Database Adapter -> RDBMS. It can also pick a template from template stack, feed data into it and pass it back to the client by adding it into the response object.

Finally the Response object via Response Middleware gets converted into HTTP Response and gets sent to the client browser over Internet by the Web Server.

This is the simplest description and panoramic view of the Django Architecture and interaction of its components at different stages while serving a client request.

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.