django 当我尝试将商品添加到购物车时,我得到了此“TypeError:unsupported operand type(s)for +=:'CartItems' and 'int'"错误

oalqel3c  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(128)

Views.py:

def add_to_cart(request, product_id):
    product = Product.objects.get(id=product_id)
    try:
        cart = Cart.objects.get(cart_id=_cart_id(request))

    except Cart.DoesNotExist:
        cart = Cart.objects.create(cart_id=_cart_id(request))
        cart.save()
    try:
        cart_item = CartItems.objects.get(product=product, cart=cart)
        cart_item += 1
        cart_item.save()
    except CartItems.DoesNotExist:
        cart_item = CartItems.objects.create(product=product, quantity=1, cart=cart)
        cart_item.save()
    return redirect('cart_app:cart_details')

字符串
我需要cart_item增加一个项目时,被添加到购物车。

u91tlkcl

u91tlkcl1#

取代

cart_item += 1

字符串
使用:

cart_item.quantity += 1

相关问题