51

In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I want to have specific urls use a set of middleware different from the global set.

hekevintran
  • 22,822
  • 32
  • 111
  • 180

9 Answers9

63

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.

gitaarik
  • 42,736
  • 12
  • 98
  • 105
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 14
    Ok, but what if I want to exclude middleware instead of append them. For example, my settings file lists middleware MIDDLEWARE_CLASSES = ('A', 'B', 'C') and I want one view to have A and B but not C. Is there a decorator to remove middleware? This custom middleware stuff is needed in only one Django app hence I don't want to have to add `decorator_from_middleware` to every other view in my application. – hekevintran May 26 '10 at 22:10
  • 2
    `@csrf_exempt`, which does something similar to what you're asking, works by setting a flag on the view which is then checked by the corresponding CSRF middleware. Not a general solution, of course, but just noting. – sfridman Nov 05 '14 at 18:39
  • How do you import the "MyMiddleware" class in Views? Tried from myapp.middleware.myFile import * but it is not picked up. Wrote a question:https://stackoverflow.com/q/52927077/6163866 – Dan Walters Oct 22 '18 at 10:23
  • How is this excluding the middleware to be only used for a particular view function, because I tried this and the middleware is being executed twice. – code_10 Jan 04 '23 at 17:16
7

I have a real solution for this issue. Warning; it's a little bit of a hack.

""" Allows short-curcuiting of ALL remaining middleware by attaching the
@shortcircuitmiddleware decorator as the TOP LEVEL decorator of a view.

Example settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',

    # THIS MIDDLEWARE
    'myapp.middleware.shortcircuit.ShortCircuitMiddleware',

    # SOME OTHER MIDDLE WARE YOU WANT TO SKIP SOMETIMES
    'myapp.middleware.package.MostOfTheTimeMiddleware',

    # MORE MIDDLEWARE YOU WANT TO SKIP SOMETIMES HERE
)

Example view to exclude from MostOfTheTimeMiddleware (and any subsequent):

@shortcircuitmiddleware
def myview(request):
    ...

"""

def shortcircuitmiddleware(f):
    """ view decorator, the sole purpose to is 'rename' the function
    '_shortcircuitmiddleware' """
    def _shortcircuitmiddleware(*args, **kwargs):
        return f(*args, **kwargs)
    return _shortcircuitmiddleware

class ShortCircuitMiddleware(object):
    """ Middleware; looks for a view function named '_shortcircuitmiddleware'
    and short-circuits. Relies on the fact that if you return an HttpResponse
    from a view, it will short-circuit other middleware, see:
    https://docs.djangoproject.com/en/dev/topics/http/middleware/#process-request
     """
    def process_view(self, request, view_func, view_args, view_kwargs):
        if view_func.func_name == "_shortcircuitmiddleware":
            return view_func(request, *view_args, **view_kwargs)
        return None

Edit: removed previous version that ran the view twice.

Chase Seibert
  • 15,703
  • 8
  • 51
  • 58
6

Here's a solution I used recently to address the scenario you presented in a comment to Ned's answer...

It assumes that:

A) this is a custom middleware or one that you can extend/wrap with your own middleware class

B) your logic can wait until process_view instead of process_request, because in process_view you can inspect the view_func parameter after it's been resolved. (Or you can adjust the code below to use urlresolvers as indicated by Ignacio).

# settings.py
EXCLUDE_FROM_MY_MIDDLEWARE = set('myapp.views.view_to_exclude', 
    'myapp.views.another_view_to_exclude')

# some_middleware.py

from django.conf import settings

def process_view(self, request, view_func, view_args, view_kwargs):
    # Get the view name as a string
    view_name = '.'.join((view_func.__module__, view_func.__name__))

    # If the view name is in our exclusion list, exit early
    exclusion_set = getattr(settings, 'EXCLUDE_FROM_MY_MIDDLEWARE', set())
    if view_name in exclusion_set:
        return None

    # ... middleware as normal ...
    #
    # Here you can also set a flag of some sort on the `request` object
    # if you need to conditionally handle `process_response` as well.

There may be a way to generalize this pattern further, but this accomplished my goal fairly well.

To answer your more general question, I don't think there is anything in the Django libraries to help you out with this currently. Would be a good topic for the django-users mailing list if it hasn't already been addressed there.

Joe Holloway
  • 28,320
  • 15
  • 82
  • 92
2

The best thing I've been able to find is using if request.path_info.startswith('...') to skip over the middleware by just returning the request. Now, you could create middleware just for the sake of skipping and then inherit that. Maybe you could do something even simpler and save that list in your settings.py and then skip all those. If I'm wrong in any way, let me know.

(Note: from what I remember, I was using Django 1.2 at this time)

dtc
  • 1,774
  • 2
  • 21
  • 44
2

You can use process_view method, that is called before calling the view func. In process_view you can check — if this view requires this middleware interception.

1

Use django.core.urlresolvers.resolve() against request.path in a wrapper for the middleware to try to see if the view is within the app, and skip processing if so.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I think this is the easy way to exclude a view from middleware

 from django.core.urlresolvers import resolve
 current_url = resolve(request.path_info).url_name

 if want to exclude url A,

 class your_middleware:
    def process_request(request):
        if not current_url == 'A':
            "here add your code"
Arjun P P
  • 39
  • 1
  • 5
0

Django urlmiddleware allows to apply middleware only to views that are mapped to specific urls.

georgebrock
  • 28,393
  • 13
  • 77
  • 72
Jan Wrobel
  • 6,969
  • 3
  • 37
  • 53
  • Nice app, however, it still adds global middleware that checks the requested url agains any configured url specific middleware: https://github.com/d0ugal/django-urlmiddleware/blob/master/urlmiddleware/middleware.py#L18 – gitaarik Dec 16 '13 at 15:37
0
#settings.py
EXCLUDE_FROM_MY_MIDDLEWARE =set({'custom_app.views.About'})

#middlware.py
from django.conf import settings

class SimpleMiddleware(object):

     def __init__(self,get_response):
          self.get_response=get_response
          
     
     def __call__(self,request):
          
          response = self.get_response(request)
          return response

     def process_view(self,request, view_func, view_args, view_kwargs):
         
          view_function='.'.join((view_func.__module__,view_func.__name__))
          exclusion_set=getattr(settings,'EXCLUDE_FROM_MY_MIDDLEWARE',set() )
          if view_function in exclusion_set:
               return None
          
          print("continue for others views")
          
     def process_exception(self,request, exception):
          return HttpResponse(exception)