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"
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.