Reputation: 99
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"
Upvotes: 0
Views: 1782
Reputation: 308939
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.
Upvotes: 2