django @login_required decorator for a superuser

Is there a decorator in django similar to @login_required that also tests if the user is a superuser?

Thanks


Solution 1:

Use the user_passes_test decorator:

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser)
def my_view(request):
    ...

Solution 2:

In case staff membership is sufficient and you do not need to check whether the user is a superuser, you can use the @staff_member_required decorator:

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

@staff_member_required
def my_view(request):
    ...