[Django]-Django: Staff Decorator

171👍

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

27👍

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

from django.contrib.admin.views.decorators import staff_member_required
from django.utils.decorators import method_decorator


@method_decorator(staff_member_required, name='dispatch')
class ExampleTemplateView(TemplateView):
    ...

11👍

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:

@staffonly
def 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.

0👍

I use Your Decorator and I face only one error:-

'bool' object is not callable

this error comes from here if u.is_authenticated() and u.is_staff:
I change u.is_authenticated() to u.is_authenticated and it nicely works for me

Leave a comment