13

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

Wickkiey
  • 4,446
  • 2
  • 39
  • 46
  • Refer this, May be it may help you. http://stackoverflow.com/questions/18322262/how-to-setup-custom-middleware-in-django – Mangu Singh Rajpurohit Nov 09 '15 at 08:07
  • @user2393267 As I requested it should be application specific. If I add the middleware it will be applicable for all applications in django project. – Wickkiey Nov 09 '15 at 08:14
  • Possible duplicate of [django settings per application - best practice?](http://stackoverflow.com/questions/22386580/django-settings-per-application-best-practice) – sobolevn Nov 09 '15 at 08:18

1 Answers1

6

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.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • In this http://python-social-auth.readthedocs.org/en/latest/pipeline.html the pipelines are made specific to particular(social) app. However now I will include the conditions in process_view .. @GwynBleidD thanks :) – Wickkiey Nov 09 '15 at 11:34