I get this error when i try accessing localhost:8000/course/u/update-item/: "AttributeError at /update_item/ 'WSGIRequest' object has no attribute 'data'"
NOTE: When i change request.data
to request.body
i get another error message that says JSONDecodeError at /course/u/update-item/ Expecting value: line 1 column 1 (char 0)
views.py
def update_item(request):
data = json.loads(request.data)
productId = data['productId']
action = data['action']
print("Action:", action)
print("ProductId:", productId)
return JsonResponse("Item was added", safe=False)
cart.js
function updateUserOrder(productId, action){
console.log('User is authenticated, sending data...')
var url = '/u/update-item/'
fetch(url, {
method:'POST',
headers:{
'Content-Type':'application/json',
'X-CSRFToken':csrftoken,
},
body:JSON.stringify({'productId':productId, 'action':action})
})
.then((response) => {
return response.json();
})
.then((data) => {
location.reload()
});
}
urls.py
path('u/update-item/', views.update_item, name="update-item"),
WSGIRequest object has no attribute 'data', In your case You have to change data attribute to POST.
data attribute is used in django rest framework. check out this