当我尝试访问localhost:8000/curse/u/update-item/时,我会收到此错误:"AttributeError在/update_item/"WSGIRequest"对象中没有属性"data">
注意:当我将request.data
更改为request.body
时,我会收到另一条错误消息,上面写着JSONDecodeError at /course/u/update-item/ Expecting value: line 1 column 1 (char 0)
视图.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对象没有属性"data",在您的情况下,您必须将data属性更改为POST。
数据属性用于django-rest框架。看看这个