Django:AttributeError at/update_item/'WSGIRequest'对象没有属性'data'

2024-04-25 06:56:42 发布

您现在位置:Python中文网/ 问答频道 /正文

当我访问本地主机时出现以下错误:8000/update\u item/:“AttributeError at/update\u item/‘WSGIRequest’对象没有属性‘data’”

views.py

def updateItem(request):
    data = json.loads(request.data)
    productId = data['productId']
    action = data['action']
    print('Action:', action)
    print('productId:', productId)

    customer = request.user.customer
    product = Product.objects.get(id=productId)
    order, created = Order.objects.get_or_create(customer=customer, complete=False)
    orderItem, created = OrderItem.objects.get_or_create(order = order, product = product)

carrito.js:

function updateUserOrder(productId, action){
    console.log('Usuario logeado y enviando data...')

    var url = '/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) =>{
        console.log('data:', data)
        location.reload()
    })

}

错误出现在以下行中:

data = json.loads(request.data)

如果我将该行更改为以下内容:

data = json.loads(request.body)

它给了我另一个错误:“JSONDecodeError at/update\u项/ 预期值:第1行第1列(字符0)”


Tags: jsondatagetobjectsrequest错误orderupdate

热门问题