Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - application specific middleware

I am aware of the following methods for adding middlewares 1) Adding custom middleware component to django using MIDDLEWARE_CLASSES

MIDDLEWARE_CLASSES = (
     '......'
    'path.to.custom.middlware',)

2) Adding view specific middleware using decorate_from_middlware

cache_page = decorator_from_middleware(CacheMiddleware)

@cache_page(3600)
def my_view(request):
    pass

My request is how to create application specific middleware class like

APPSPECIFIC_MIDDLEWARE_CLASSES = ( 'path.to.middlwareclass1',
 'path.to.middlwareclass2',
 'path.to.middlwareclass3', )

middlwareclass is either function or class ? Is there any to do this using url or any other method,. Or 2nd method is the only way and to add all middleware classes separately to the view ?

Update : http://python-social-auth.readthedocs.org/en/latest/pipeline.html As in the about application, the SOCIAL_AUTH_PIPELINE is working for the social app alone. which differs from the global project settings..

Thanks in Advance

like image 335
Wickkiey Avatar asked Nov 09 '15 08:11

Wickkiey


1 Answers

This is probably not possible in way that you want it, because django does not track what view comes from what application. But you can create some middleware with condition inside process_view, you can check here what view is being called and do something if view is matching your criteria (in that case, view comes from particular app).

Another approach, if you're using class-based views is to create some view mixin or base view in your application, decorate it with your middleware and use it in each view from your application.

like image 113
GwynBleidD Avatar answered Oct 23 '22 15:10

GwynBleidD