Building Web Applications in Django from Scratch : Part 2

Devang Mehta
4 min readFeb 20, 2023

--

request-response cycle and routing urls in Django

In the first part of this series, we learnt about the basics of Django, its architecture, created a new project and understood all the essestial files that are present in a Django project and its apps. Hopefully, that might have given you an idea about how to start working with Django and prepare the basic setup. Today, we will continue where we had left and give our application a working body by writing views (business logic) and models (database).

Request-Response Cycle in Django

When a client(e.g web browsers) need information from a server, they usually send a request. A request is basically a set of information that helps the server understand what the client requires and based upon this information the server generates its response. This whole process, from receiving a request from the client to generating a response and sending it back is know as the request-response cycle.

In Django, an incoming request passes through various components that help in routing, validating and generating response for the request. We shall now study each of these components one-by-one and understand their necessecity.

  • WSGI File: This is a Web Server Gateway Interface for your Django project. It is a standard that facilitates the communication between the web server and web applications.
  • middlewares: Each request, by default, passes through a set of middlewares that are defined in the settings.py file. The middlewares help in validating the request and do a pre-check before sending them further. Each middleware is processed one at a time in a top-down order (as defined in the settings.py file) and if any of them fails the request is sent back with apt response from there only.
  • URL Routers: In Django, url routing is defined in urls.py file. The job of the url routers is to analyse the pattern of the incoming request url and map them to the correct view. If the router is unable to find a suitable match for the incoming request, the request is sent back and does not reach the views at all.
  • Views: As discussed in the last part, views contain the business logic of an application in Django and are defined in views.py file of each app. They are the ones to generate the responses desired by the client, if the request is able to pass through all the components defined above.
  • Models: Models are the components through which we interact with the database. If the request contains any operation that requires database interaction, the view talks to the database through the models.

Once a request passes through all the above components successfully, the views generate a response object that goes through the response middlewares before being sent to the client. This is how the complete request-response cycle looks in Django

One thing to note here is that we have ommited template layer from the request-response cycle as we will only be focussing on the models and views in this series.

Mapping Requests to Views in urls.py

As mentioned earlier, the urls.py file is used to route incoming requests to the corresponding views. But how do we write these routings and how does Django know which view to connect to?

Django creates a urls.py file by default in the project directory which routes all the requests, but in each app we have the flexibility to create routing files of their own. This helps in proper structuring and enhancing modularity of the code.

Each route defined in the urls.py file is basically a path that may contains regular expressions. Django tries to match the incoming requests with the paths defined in file and if there is a match, forwards the request to the view.

For Example, if we have a view function named View1, then the url route may look like this:

from django.urls import path, url
from . import views

urlpatterns = [
url(r'api/view/', View1, name='View1'),
]

Whenever a request comes in the format <hostname>/api/view/ Django knows that this should be relayed to the view View1. We might discuss writing urls and regex expressions in depth in some other article.

Now that we have got an understanding of the request-response cycle and url routing in Django, the next things to work upon are writing views and creating database tables. We will start with Django ORM in the next article of this series and learn how to create and interact with database tables using django.

If you liked this article, do follow me for more contents like this. I have another blog at https://devangmehta1997.wordpress.com/ which you can follow for similar content.

Thank you 🙏

--

--

Devang Mehta

Engineering Lead | Interested in tech, history, philosophy and psychology