user5594493
user5594493

Reputation: 1130

Class based Views not recognizing PUT and DELETE methods

I am using class based views and calling PUT , delete methods via ajax but Django is not recognizing these methods and giving 403 error. But according to this it should support these methods

My code:

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 fails to recognize put and delete in above view

Edit :

I am using two mixins here;

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)

Upvotes: 1

Views: 927

Answers (1)

Martin Massera
Martin Massera

Reputation: 1912

Make sure you have the CSRF token, same as you would with POST.

I also had the same problem, made a DELETE request and got 403 and checked in the chrome console and got this:

enter image description here

Upvotes: 0

Related Questions