问题
Is there any feature available in django so that we can combine some filtering on all actions in the view of django, like before_filter: is available in rails.
回答1:
No. before_, around_ and after_ filter concepts aren't present in Django, but it isn't hard to write your own functions to do the same thing. There are also signals and generic views that might accomplish what you're needing to do.
回答2:
I'm still learning Rails but from what I've observed so far python decorators also seem to be used in Django in a very similar way to the before_filter in Rails.
Here's one example of it's usage in authenticating users: https://docs.djangoproject.com/en/1.2/topics/auth/#the-login-required-decorator
来源:https://stackoverflow.com/questions/6584512/is-there-before-filter-in-django-as-in-rails