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"),
traceback
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 13, 2022 - 11:24:22
Django version 3.1, using settings 'dnextedprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /course/u/update-item/
Traceback (most recent call last):
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Destiny\Desktop\Circus\dnextedprj\course\views.py", line 90, in update_item
data = json.loads(request.data)
AttributeError: 'WSGIRequest' object has no attribute 'data'
[13/Apr/2022 11:24:26] "GET /course/u/update-item/ HTTP/1.1" 500 63699