← Back to Django Course | Chapter 12: Signals & Middleware | Lesson 5 of 8

Django Middleware का परिचय

Middleware checkpoints की एक series जैसी है जिससे हर request अपने view तक और वापस जाते समय गुज़रती है।
Syntax
markup
class MiddlewareName:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # code before the view
        response = self.get_response(request)
        # code after the view
        return response

Middleware क्या है?

Middleware एक class है जो Django के request handling और आपके view के बीच बैठती है, view execute होने से पहले और बाद में code चलाने में सक्षम।

उदाहरण: What Is Middleware?

Middleware is a class that sits between Django's request handling and your view, able to run code before and after the view executes.

markup
class SimpleLoggingMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print(f"Request path: {request.path}")
        response = self.get_response(request)
        return response
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Request/Response Chain

self.get_response(request) call करना next middleware (या view) को control hand करता है। उस call से पहले का code आते समय चलता है; बाद का code जाते समय चलता है।

उदाहरण: The Request/Response Chain

Calling self.get_response(request) hands control to the next middleware (or the view). Code before that call runs on the way in; code after runs on the way out.

markup
class TimingMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print("Before view runs")
        response = self.get_response(request)
        print("After view runs")
        return response
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Built-in Middleware जो आप पहले से उपयोग करते हैं

हर नया Django project security headers, sessions, और authentication के लिए middleware के साथ आता है जो settings.py में पहले से enabled है।

उदाहरण: Built-in Middleware You Already Use

Every new Django project ships with middleware for security headers, sessions, and authentication already enabled in settings.py.

markup
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Middleware कहां रहता है

Middleware settings.py में एक dotted Python path के रूप में register होता है, चाहे यह Django का अपना हो या आपने खुद लिखा हो।

उदाहरण: Where Middleware Lives

Middleware is registered in settings.py as a dotted Python path, whether it's one of Django's own or one you wrote yourself.

markup
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "myapp.middleware.SimpleLoggingMiddleware",
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह सोचना कि middleware सिर्फ view से पहले चलता है — यह response के साथ वापस जाते समय भी चलता है।
  2. यह भूल जाना कि settings.py में MIDDLEWARE का order request/response processing order तय करता है।
  3. Middleware में heavy काम करना जो हर single request पर चलता है, भले ही उन्हें इसकी ज़रूरत न हो।
चैप्टर सारांश
  • Middleware Django में हर request/response cycle को wrap करता है।
  • हर middleware view चलने से पहले request को और बाद में response को inspect या modify कर सकता है।
  • Django security, sessions, और authentication के लिए useful built-in middleware के साथ आता है।
  • settings.py में MIDDLEWARE का order मायने रखता है — यह एक chain बनाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.