[Python] getting error as "home.models.Cart.DoesNotExist: Cart matching query does not exist."

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    Views.py code:

    def plus_cart(request):
    if request.method == "GET":
    pk = request.GET.get('prod_id')
    c = Cart.objects.get(pk=pk, user=request.user)
    c.quantity+=1
    c.save(update_fields=['quantity'])
    user = request.user
    cart = Cart.objects.filter(user=user)
    amount = 0
    for p in cart:
    value = p.quantity * p.product.dp
    amount = amount + value
    totalamount = amount + 40
    data={
    'quantity':c.quantity,
    'amount':amount,
    'totalamount':totalamount,
    }
    return JsonResponse(data)


    in urls.py, following url is added: path('pluscart/', views.plus_cart),

    in the static folder, myscript.js has following code:

    $('.plus-cart').click(function(){
    var id=$(this).attr("pid").toString();
    var eml=this.parentNode.children[2]
    console.log("pid= ", id)
    $.ajax({
    type:"GET",
    url:"/pluscart",
    data:{
    prod_id:id
    },
    success:function(data){
    console.log("data= ", data);
    eml.innerText=data.quantity
    document.getElementById("amount").innerText=data.amount
    document.getElementById("totalamount").innerText=data.totalamount
    }
    })
    })


    while querying the database the python manage.py shell, user and cart_item with the required prod_id checked and found ok. but error is generated while executing the code. Please help.

    Continue reading...

Compartilhe esta Página