Django Request and Response Lifecycle

Lokesh Chinni
3 min readJul 6, 2017

--

Django is a web application framework, which is of the type MTV formally Model Template View. In Django request and response flow is the collaboration between user and application. These days, most web application is done with python web frameworks like Django and Flask. Here are the components to make your web development journey success.

1. Webserver and wsgi

2. Middlewares

3. urlconf

4. View

5. Template

Webserver and WSGI:

Web client will raise request, that directly goes to web server. Now it’s the job of webserver and wsgi, Django has a layer Web Server Gateway Interface (wsgi) to accept requests and send the response back once the request served by application.

Middlewares:

Django middlewares are lightweight components for modifying request that comes in and template response that goes out. There are a set of middleware classes, which by default comes with Django library.

The order of middleware really matters, because Django executes the same way. Middlewares comes in the below flavors request middleware, view, error, template and response.

1. Request Middleware: Request middleware will accept all incoming requests before they are directed to a view. At this point of time, Django does not know from which type of device this request has raised, but it is sure that the request came from WSGI. User can customize the request.

2. Response Middleware: It will return a HttpResponse object or None. If it returns None, the execution continues. If it returns an HttpResponse, the response will go back to the WSGI layer and no other middleware or views will be called for the current context.

URL Conf:

URL configuration or urlconf is one of the important players in Django workflow. Urlconf is a configurtion unit, it will look for the url file. In the url file, all the routes and respective views are present. By the time WSGI gets a request, urlconf will identify it and executes the target view.

View:

View is the place where the developer writes logic to interact backend and business logic. From Django perspective, view will work like a controller, whatever the operations done by controller. The it generates the final result. Inside view different types of requests (GET, POST) can be handled differently. Now result is ready for dispatch. View always accepts HttpRequest as an object and returns HttpResponse object.

Template:

Views generate pages that users see in the templates. These Templates are HTML pages with a bit of python to make it dynamic. Views can pass a dictionary of data formally called as CONTEXT OBJECT to a template. The template can access the data items by using keys. Take an example, the context for this page contains a key called number_of_hits. If I want to show the Like Count somewhere in the HTML, I can refer to the key with {{number_of_hits}} in my template.

Hope you enjoy reading this post on Django. Do let me know your thoughts writing to me at Lokesh.chinni@suneratech.com or use the comments section below.

For more details on Software services or software applications support, visit http://www.suneratech.com/

--

--