簡體   English   中英

Django: AttributeError at /update_item/ 'WSGIRequest' object 沒有屬性 'data'

[英]Django: AttributeError at /update_item/ 'WSGIRequest' object has no attribute 'data'

當我訪問我的 localhost:8000/update_item/ 時出現以下錯誤:“AttributeError at /update_item/ 'WSGIRequest' object has no attribute 'data'”

視圖.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_item/ Expecting value: line 1 column 1 (char 0)”

試試這個而不是 request.data

def updateItem(request):

     if request.method =='POST':
            productId = request.POST['productId'] 
            action = request.POST['action ']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM