Non-global middleware in Django

前端 未结 8 1005
生来不讨喜
生来不讨喜 2020-12-12 21:59

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

8条回答
  •  轮回少年
    2020-12-12 22:40

    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.

提交回复
热议问题