Middleware is an arrangement of aides into Django’s request/response planning. It’s a light, low-level “module” structure for all around altering Django’s data or yield.
Each middleware part is accountable for doing some specific limit. For example, Django joins a middleware section, AuthenticationMiddleware, that accomplices customers with requesting using sessions.
This record clears up how middleware capacities, how you impel middleware, and how to create your own particular middleware. Django ships with some understood middleware you can use suitable out of the case. They’re filed in the verifiable middleware reference.
Changed in Django 1.10:
Another style of middleware was exhibited for use with the new MIDDLEWARE setting. In the event that you’re using the old MIDDLEWARE_CLASSES setting, you’ll need to modify old, custom middleware before using the new setting. This file depicts new-style middleware. Suggest this page in more settled interpretations of the documentation for a delineation of how old-style middleware capacities.
Creating your own specific middleware¶
A middleware preparing plant is a callable that takes a get_response callable and gives back a middleware. A middleware is a callable that takes a request and returns a response, much the same as a view.
A middleware can be made as a limit that takes after this:
# One-time outline and presentation.
# Code to be executed for each request some time as of late
# the view (and later middleware) are called.
response = get_response(request)
# Code to be executed for each request/response after
# the view is called.
Or, then again it can be formed as a class whose events are callable, like this: