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)
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
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( , { headers: { Authorization: 'Token token' }, }) .then(res => { console.log(res) })
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.