7

例如,我有一个基于类的视图,它允许 GET 和 POST 方法,如下所示,

class ViewOne(View):
    def post(self, request, *args, **kwargs):
        ...
    def get(self, request, *args, **kwargs):
        ...
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ViewOne, self).dispatch(*args, **kwargs)

现在,GET 和 POST 都是 login_required。但是如果我只希望 POST 是 login_required 怎么办?

4

2 回答 2

4

嗯...它不起作用吗?

class ViewOne(View):
    @method_decorator(login_required)
    def post(self, request, *args, **kwargs):
        ...
    def get(self, request, *args, **kwargs):
        ...    
于 2012-12-19T13:03:36.460 回答
1

为什么不创建两个类,也使用django-braces ;)

class ViewOne(View):
    def get(self, request, *args, **kwargs):
    ...

class ViewTwo(LoginRequiredMixin, ViewOne):
    def post(self, request, *args, **kwargs):
    ...
于 2012-12-19T13:14:37.317 回答