Django custom middleware introduction
Hi, in this article I am going to explain the simple introduction on
- Django middleware
- Built-in Django middleware
- custom middleware
- Django middleware
a) What is Django middleware?
In django, middleware is a framework of attached into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input or output. It allows you to plug in extra code that will be executed at specific points of the HTTP request/response cycle
b) Why it is required?
because of the attach nature of the middleware some time we need to add additional functionality while processing every incoming request and every out going response for example user in request.user based on token or session using AuthenticationMiddleware, elimination blacklisted ip address, attach aditional information, etc..
c) How does it work?
An example of this is the SessionMiddleware, which is used to associate sessions to users. when http request made midldleware will execute like onion type layer like one by one, when execution flow enter into your middleware process_request method will execute after that
get_response
callable called and returns a middleware…