0
0
DjangoHow-ToBeginner · 3 min read

Add Custom Header in Django Middleware: Simple Guide

To add a custom header in Django middleware, create a middleware class with a __call__ or process_response method that modifies the response object by adding your header using response['Header-Name'] = 'value'. Then add this middleware to your settings.py MIDDLEWARE list.
📐

Syntax

In Django, middleware is a class that processes requests and responses. To add a custom header, you modify the response object inside the middleware's __call__ or process_response method.

  • __init__(self, get_response): Initializes middleware with the next callable.
  • __call__(self, request): Processes the request and gets the response.
  • response['Header-Name'] = 'value': Adds the custom header to the response.
python
class CustomHeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response['X-Custom-Header'] = 'MyValue'
        return response
💻

Example

This example shows a complete middleware class that adds a custom header X-Custom-Header with the value MyValue to every HTTP response.

After creating this middleware, add its path to the MIDDLEWARE list in settings.py to activate it.

python
class CustomHeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response['X-Custom-Header'] = 'MyValue'
        return response

# In settings.py
# MIDDLEWARE = [
#     ...
#     'yourapp.middleware.CustomHeaderMiddleware',
# ]
Output
When you make any HTTP request to your Django app, the response headers will include: X-Custom-Header: MyValue
⚠️

Common Pitfalls

Common mistakes when adding custom headers in Django middleware include:

  • Not returning the response object after adding the header.
  • Adding headers in the process_request method, which does not have access to the response.
  • Forgetting to add the middleware class path to the MIDDLEWARE list in settings.py.

Always add headers after calling get_response(request) to ensure the response object exists.

python
class WrongMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Incorrect: modifying request instead of response
        request.META['X-Custom-Header'] = 'MyValue'
        response = self.get_response(request)
        return response

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

    def __call__(self, request):
        response = self.get_response(request)
        response['X-Custom-Header'] = 'MyValue'
        return response
📊

Quick Reference

Summary tips for adding custom headers in Django middleware:

  • Use __call__ method to access and modify the response.
  • Add headers by setting response['Header-Name'] = 'value'.
  • Always return the modified response.
  • Register your middleware in settings.py under MIDDLEWARE.

Key Takeaways

Add custom headers by modifying the response object inside middleware's __call__ method.
Always return the response after adding headers to ensure they are sent to the client.
Register your middleware class in the MIDDLEWARE list in settings.py to activate it.
Do not try to add headers in request processing methods; headers belong to responses.
Test your middleware by checking response headers in browser DevTools or curl.