基于类的视图无法识别PUT和DELETE方法

时间:2016-03-07 09:45:58

标签: python django django-class-based-views

我正在使用基于类的视图并调用PUT,通过ajax删除方法,但Django没有识别这些方法并给出403错误。但根据this,它应该支持这些方法

我的代码:

class MynDetail(TokenRequiredMixin, View):
    def post(self, request, *args, **kwargs):
             ''my code here''  

    def put(self, request, *args, **kwargs):
        ''my code here''

    def delete(self, request, *args, **kwargs):
        ''my code here''

Django无法识别上面视图中的put和delete

修改:

我在这里使用两个mixins;

class TokenRequiredMixin(object):
    """
    check if user token cookie exist or not
    """
    def dispatch(self, request, *args, **kwargs):
        if 'user_token' not in request.COOKIES.keys():
            return redirect(reverse('login-view'))
        else:
            url = get_base_url(request, 'v1')
            valid_token = check_token_validity(url, request.COOKIES.get('user_token'))
            if valid_token.get('token') is None:
                response = redirect(reverse('login-view'))
                response.delete_cookie('user_token')
                response.delete_cookie('user_uid')
                return response
        return super(TokenRequiredMixin, self).dispatch(request, *args, **kwargs)


class OrgValidateMixin(object):
    """
    check if user have created an organisation or not
    If not, then redirect him to create org page
    if yes, then he must not able to create another organisation
    so he can't access create organisation page.
    """

    def get_token(self):
        return self.request.COOKIES.get('user_token')


    def get_organisation(self):
        token = self.get_token()
        url = get_base_url(self.request, 'v1')
        return check_user_org(url, token)

    def dispatch(self, request, *args, **kwargs):
        user_org_exists = self.get_organisation()
        current_path = request.get_full_path()
        create_org_path = reverse('create-org-view')

        if (current_path != create_org_path) and not user_org_exists :


Edit : 

 return redirect(reverse('create-org-view'))
    elif (current_path == create_org_path) and user_org_exists :
        return redirect(reverse('portal-dashboard'))

    return super(OrgValidateMixin, self).dispatch(request, *args, **kwargs)

1 个答案:

答案 0 :(得分:0)

确保您拥有CSRF令牌,与POST相同。

我也遇到了同样的问题,提出了DELETE请求并获得了403并在chrome控制台中检查并得到了这个:

enter image description here