0
0
DjangoConceptBeginner · 3 min read

What is process_request in Django Middleware Explained

In Django middleware, process_request is a method that runs before the view function is called. It lets you inspect or modify the incoming HttpRequest object or return a response early, stopping further processing.
⚙️

How It Works

Think of Django middleware as a series of checkpoints that every web request passes through before reaching your view. The process_request method is like the first checkpoint where you can look at the request details and decide what to do next.

When a user sends a request, Django calls process_request on each middleware in order. You can use this method to check things like user authentication, modify headers, or even block the request by returning a response immediately. If you return a response here, Django skips calling the view and sends that response back to the user.

This is similar to a security guard checking IDs at the entrance of a building. If the ID is valid, the visitor proceeds inside (the view). If not, the guard stops them right away (returns a response).

💻

Example

This example middleware checks if a custom header X-Blocked is present in the request. If it is, it returns a simple response denying access. Otherwise, it lets the request continue to the view.

python
from django.http import HttpResponse

class BlockHeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Call process_request before view
        response = self.process_request(request)
        if response:
            return response
        # Continue processing
        response = self.get_response(request)
        return response

    def process_request(self, request):
        if request.headers.get('X-Blocked') == 'true':
            return HttpResponse('Access denied by middleware.', status=403)
        return None
Output
If a request has header 'X-Blocked: true', the response is 'Access denied by middleware.' with status 403; otherwise, the request proceeds normally.
🎯

When to Use

Use process_request when you want to act on a request before it reaches your view. Common uses include:

  • Checking authentication or permissions early
  • Redirecting users based on request data
  • Blocking requests with invalid headers or IP addresses
  • Adding or modifying request attributes for later use

This helps keep your views clean and centralizes request handling logic.

Key Points

  • Runs before the view: process_request is called before Django calls your view function.
  • Can return a response: Returning a response stops further processing and sends it immediately.
  • Modify request: You can change the request object before it reaches the view.
  • Part of middleware: Must be defined inside a middleware class following Django's middleware pattern.

Key Takeaways

process_request runs early to inspect or modify incoming requests before views.
Returning a response in process_request stops the view from running and sends that response.
Use it to block, redirect, or add data to requests centrally.
It helps keep your app organized by handling common request checks in one place.