django @login_required decorator for a superuser

后端 未结 7 1435
忘了有多久
忘了有多久 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:12

    I recommend using Mixins, example:

    from django.contrib.auth.mixins import UserPassesTestMixin
    
    
    class SuperUserCheck(UserPassesTestMixin, View):
        def test_func(self):
            return self.request.user.is_superuser
    

    Then you can add SuperUserCheck to View class:

    class MyView(SuperUserCheck, View):
    

提交回复
热议问题