0

when i use user_passes_test decorators an error display :

"AttributeError: 'function' object has no attribute 'as_view'"

this is my code :

urls.py :

url(r'^user/admin/$', UpdateAdminView.as_view(), name='admin'),

views.py :

@user_passes_test(lambda u: u.is_superuser)
@method_decorator(login_required, name='dispatch')
class UpdateAdminView(TemplateView):
    template_name = "admin.html"
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Ryuuk
  • 89
  • 1
  • 3
  • 14
  • You should put method decorator. check this https://stackoverflow.com/questions/6069070/how-to-use-permission-required-decorators-on-django-class-based-views – Rohan Oct 23 '17 at 11:32

1 Answers1

2

You should use the method decorator for your superuser check, just as you do for login required.

Since a user must be logged in to be a superuser, you can remove the login_required decorator in this case.

superuser_required = user_passes_test(lambda u: u.is_superuser)

@method_decorator(superuser_required, name='dispatch')
class UpdateAdminView(TemplateView):
    template_name = "admin.html"

You may want to look at UserPassesTestMixin as an alternative for class based views.

Alasdair
  • 298,606
  • 55
  • 578
  • 516