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
增加一个项目时,被添加到购物车。
1条答案
按热度按时间u91tlkcl1#
取代
字符串
使用:
型