2

I am using ReactJS as client side web app and I'm using axios package. In my backend, I am using Django Rest Framework. I created Serializer for CartItem Model:

class CartItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartItem
        # Fields you want to be returned or posted
        fields = '__all__'

Viewset:

class CartItemViewSet(viewsets.ModelViewSet):
    queryset = CartItem.objects.all()
    serializer_class = CartItemSerializer

I am trying to use default delete method of DRF in axios using so:

axios.delete('cart_items/', {
                headers: { Authorization: 'Token token' },
                data: {
                    id: 1,
                },
            })
            .then(res => {
                console.log(res)
            })

When I call that, it gives me error in React: DELETE http://127.0.0.1:8000/cart_items/ 405 (Method Not Allowed)

2 Answers 2

3

The problem lies in your URL. The URL should point toward the CartItem instance (the URL of DetailView)

So, The URL should be

http://127.0.0.1:8000/cart_items/123/
Where, the 123 is the PK of the instance to be deleted. Also you don't have to attach the payload to the request since it has no effect on DRF side.


axios.delete('cart_items/1/', {
    headers: { Authorization: 'Token token' },

})
    .then(res => {
        console.log(res)
    })
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This fixed the problem. :)
0

The error is in this line

axios.delete('cart_items/',

You have to provide the url of rest endpoint like 'localhost:port/cart_items/' if the server is running locally on some port

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.