Non-global middleware in Django

You want decorator_from_middleware.

from django.utils.decorators import decorator_from_middleware

@decorator_from_middleware(MyMiddleware)
def view_function(request):
    #blah blah

It doesn’t apply to URLs, but it works per-view, so you can have fine-grained control over its effect.

Leave a Comment