django @login_required decorator for a superuser

后端 未结 7 1436
忘了有多久
忘了有多久 2020-12-01 02:21

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

Thanks

7条回答
  •  伪装坚强ぢ
    2020-12-01 03:01

    To require a superuser on a class based view without writing new code:

    from django.utils.decorators import method_decorator
    from django.contrib.auth.decorators import user_passes_test
    
    @method_decorator(user_passes_test(lambda u: u.is_superuser), name='dispatch')
    class AdminCreateUserView(LoginRequiredMixin, FormView):
        ...
        ...
        ...
    

提交回复
热议问题