Django: AttributeError at /course/u/update-item/ 'WSGIRequest' object has no attribute 'data' using django

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

The attribute is request.body, not request.data. If you’re getting the error using it, that’s a different issue.

when i use request.body is shows this error JSONDecodeError at /course/u/update-item/ Expecting value: line 1 column 1 (char 0)

Check the network tab in your browser’s developer tools to verify that your javascript is sending what you think is being sent. (Or put a print statement in your view to print the body before trying to decode it.)

i just checked and i’m getting a 500 internal server error, it think my javascript updateUserOrder function doesn’t send data to django, is there a way i can fix this?

This just jumped out at me from the log.

For some reason you’re trying to GET from that url instead of POSTing to it.
Something in your system is issuing a GET.

(also, the url in your code is referencing ‘/u/update-item/’ and not ‘/course/u/update-item/’)

It appears to me that the error is somewhere other than this block of JavaScript.

1 Like

Yes, i was supposed to post to the url but i was GETting instead, thanks i fixed it

1 Like

where exacty do you make the changes from Get to POST

Hello where do i make the changes from a GET request to a POST request

That depends upon where and how you’re doing the GET.

1 Like

show exactly where you made the change