Django Middleware का परिचय
In this page:
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.
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.
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.
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.
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. #}
- यह सोचना कि middleware सिर्फ view से पहले चलता है — यह response के साथ वापस जाते समय भी चलता है।
- यह भूल जाना कि settings.py में MIDDLEWARE का order request/response processing order तय करता है।
- 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 बनाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: