![]() |
VOOZH | about |
One of the key and must feature of Django is the concept of middleware, which allows us to process requests and responses across the entire web application. Middleware in Django acts as a layer between the requests and view or between the view and the response, including it's useful for tasks like logging, authentication, modifying headers, etc.
In this article we are going to deep dive into the knowledge of custom middleware in Django.
MiddleWare in Django is a light-weight, modular with low-level of "plugin" dependencies that processes requests and responses globally across our application. It is activated whenever we a request flows through the application, making it powerful tool for cross-cutting concerns such as:
In Django, middleware is a python class which is executed as follows:
To create a custom middleware in Django it involves writing a python class which implements at least one of the following methods.
For this article particularly, we are going to create a middleware which is going to log the time how much a request takes to process, which is useful for performance monitoring.
pip install django
django-admin startproject middleware_project
cd middleware_project
python manage.py startapp myapp
In myapp/middleware/request_time_logging.py, add the following middleware.
In middleware_project/settings.py
myapp/views.py
Now, we set up the URL routing for the view in myapp/urls.py:
Next, similarly we include myapp's URLs in the main project's urls.py(middleware_project/urls.py)
Now that the middleware and view are set up done, we can run the server and see the middleware in action
Run the Server:
python manage.py runserver
Web Output:
Console Output:
Each time we refresh the browser we get the log of the time taken to processs the output
With Great power comes Great responsibiliity, while middleware is powerful, but to handle this we must take several best practices which maintain the flow of developement process keeping in mind like a production grade applications.
Middleware though is the powerful way used for logging, authentication, authorization etc., we must need to keeo them as light as possible beacause for every execution they need to do request and response, so it is crucial to avoid performance heavy tasks inside.
Make sure our middleware must able to handle all exceptions ny mantaining integrity. This is especially important if our middleware is modifying the request or response as per the execution requirements.
Middleware is executed in the order it is listed in Middleware, means one by one linearly, we always be careful of this when defining custom middleware, as it can affect how different middleware component interacts.
We must keep our middleware resuable by making it as modular as possible by every means. This will allow us to use it across different projects.
Although we can able to modify the request object in middleware, it is generally advisable to maintain it's original structure for consistency, easy maintainenece and reliabilty in views.
We finally created a Django Project with the usage of custom middleware that logs the time takes to process each request. This is a powerful way to monitor the performance of the web app created using Django which helps in handling the cross-cutting concerns such as logging, authentication, or security checks across all views.