问题
It happens a lot, when all the views in a specific module are supposed to be available only when the user is authorized, or they should all do the same checks.
How could I avoid repeating the annotations all over the file?
回答1:
When using class-based views you can create a base class/mixin for all these views which implements the desired functionality (also using decorators) and then have all the views inherit from this base view.
from django.views.generic import TemplateView
class BaseView(TemplateView):
def get(self, request, *args, **kwargs):
# do some checking here
if not request.user.is_authenticated():
# do something if anonymous user
return super(BaseView, self).get(request, *args, **kwargs)
class MyView(BaseView):
pass
回答2:
In your urls
url(r'someregexp/$', mydecorator(view.myview.dude), 'name_of_view'),
回答3:
You could write a dispatcher, but if you have many urls for a module it would probably be more convenient to create a middleware layer.
See also: decorating-all-django-admin-views-1-4
回答4:
When many urls need to be added, better wrap the decorator inside a function and call that function.
from django.conf.urls import re_path
from . import views
from somewhere import decorator
def url_with_decorator(regex, view, kwargs=None, name=None):
return re_path(regex, decorator(view), kwargs, name)
urlpatterns = [
url_with_decorator(r'^$', views.index, name='index'),
url_with_decorator(r'^any_thing$', views.any_view, name='index'),
# TODO: add url here
]
来源:https://stackoverflow.com/questions/16664916/how-to-apply-a-decorator-to-all-views-of-a-module-in-django