Django custom middleware introduction

Hemanth S P
Django lions ✨
Published in
2 min readMay 1, 2020

--

django custom middleware
Photo by Wilhelm Gunkel on Unsplash

Hi, in this article I am going to explain the simple introduction on

  1. Django middleware
  2. Built-in Django middleware
  3. custom middleware
  4. 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…

--

--