What is Django Middleware | Conceptual Guide | Django Tutorials
June 2, 2021 2022-12-19 7:09What is Django Middleware | Conceptual Guide | Django Tutorials
What is Django Middleware | Conceptual Guide | Django Tutorials
Introducing Discussion | What is Django Middleware
What is Django middleware? It is a very common question, we have discussed in this Django tutorial all about it, you have to read this tutorial from start to end if you want to learn properly. Django M/W is the lightweight plugin that works in Django request/response. We can use Django’s built-in functionality in M/W as well as we can also use custom functionality. You know, if we need any kind of functionality, we get a plugin for that specific functionality, Same like that, here we also get help from the M/W plugin to include some functionality in the Django project. Just we have to plug in or plug out.
We can also define it as according to the official documentation
“Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input or output.”
In short, if you are using Django middleware, it must perform any function in your Django project in request/response processing.
What is Django Middleware according to JafriCode?
Actually, it is the combination of two words Middle and ware. If we defined both words then we can understand conceptually what is Django Middleware in fact. So middle refers to place between two points. For example, we go from point A to point C, but there is another point between A and C, that is B. So B is the middle point between A and C.
As the other word is ‘ware’. Ware means something or objects. So we can define it as the things or objects or anything else between two points.
Actually definition:
We define it in terms of Django web development, then we can define it as’ Middleware is the request/ response processes between two points that are user and server. The data that is requested by the user and provided by the server is the things or objects and as it is working between two points user and server are middle. So you have understood I hope. Any question?
DJ Middleware Types
There are generally two types of M/W
- Built-in
- Custome
If you check Django settings.py file you will find all the built-in M/W as shown in the following code.
MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, ‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]
It is middleware’s list in which different M/W are listed performing different functionalities as for security purposes request/response processing, Django by default adds an M/W ‘Django.middleware.security.SecurityMiddleware’, so others also are included as for session, CSRF, Authentication, Message functionalities, etc.
Where Django Middleware Located?
You will find it in settings.py > MIDDLEWARE = [‘…’]
When you create a new Django project, you get different files by default as urls.py settings.py, etc. In settings.py you will find the middleware list in which all the custom and built-in middleware will be located there. You can plug in and out any middleware, it depends upon your needs.
How DJ Middleware works
When you send a request to the server it will not send your request directly to the server but it will pass through different middleware then finally reach to server. The middleware will perform the task for which they develop before going to the server. Sometimes, a user sends a request to the server but the user sees a message on the screen that the website is in maintenance mode. It means, your request did not go to the server and the server did not give any response. This response (“maintenance mode”) is given by middleware so, you have understood it work between request and response processing.
Conclusion
I sure, you have understood what is Django Middleware, its working, and more about it. You should know, these Django tutorials about M/W are very short and to the point. We will share knowledge with you in the next tutorials as to how to create custom middleware, it’s detailed working, activating and deactivating middleware, etc.
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
After reading today’s topic, if you face any problem while reading, understanding please discuss it with us. If you have understood and have any relevant questions then please discuss with us in the following comment section.
Recommended Django tutorial for beginners
What is Django URL Tag in Django Template Language
Complete Discussion on Django Forms Widget | Django Tutorials
FULL Guide | Django messages Framework
Hack – Django Applications Examples | Complete Django Tutorials