Posts

Showing posts with the label middlewares

What is middleware in django?

Image
Middleware  : Middleware is a framework of hooks into Django’s request/response processing. Middleware is a lightweight plugin for Django. It allows modifying request or response or the view parameters. Each middleware component is responsible for doing some specific function. For example, Django includes a middleware component, AuthenticationMiddleware, that associates users with requests using sessions. When to use middleware  : We can use middleware if you want to modify the request i.e HttpRequest object which is sent to the view. Or you might want to modify the HttpResponse object returned from the view. Both these can be achieved by using middleware. We might want to perform an operation before the view executes. In such case, we would use a middleware. Hooks and Application order  : Request phase  : During the request phase, before calling the view. Django applies middleware in the order it's defined in MIDDLE_CLASSES top-down. 2 hooks are ava...