permission_required decorator not working for me

前端 未结 5 1114
长情又很酷
长情又很酷 2021-02-04 18:24

I can\'t figure out why the permission required decorator isn\'t working. I would like to allow access to a view only for staff members. I have tried

@permission         


        
5条回答
  •  隐瞒了意图╮
    2021-02-04 18:38

    By the way, if you're using class-based views, you should wrap your decorator in the method_decorator decorator (go figure):

    class MyView(DetailView):
        ...
        @method_decorator(permission_required('polls.can_vote', login_url=reverse_lazy('my_login')))
        def dispatch(self, request, *args, **kwargs):
            .... blah ....
    
    class MyModel(models.Model):
        ...
        def has_perm(self perm, obj=None):
            if perm == 'polls.canvote':
                return self.can_vote()
    

提交回复
热议问题