Django: Staff Decorator Django: Staff Decorator django django

Django: Staff Decorator


This decorator already exists as

from django.contrib.admin.views.decorators import staff_member_required@staff_member_required

Trunk:http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/views/decorators.py


For Class Based Views, you can decorate the view class's dispatch method like this:

from django.contrib.admin.views.decorators import staff_member_requiredfrom django.utils.decorators import method_decorator@method_decorator(staff_member_required, name='dispatch')class ExampleTemplateView(TemplateView):    ...


This style of decorator function is used with a parameterised decorator - eg when you do:

@staffonly(my_arguments)def function(request):    blah

If you're not actually calling the outer function, ie you're using it like this:

@staffonlydef function(request):

You will get odd results, as the function object will be passed to the wrong one of the nested functions in the decorator.