Python 添加到购物车数量Django
我正在尝试更新购物车上的数量,但它实际上没有更新数量。我的购物车基于模型,但我认为问题在于:Python 添加到购物车数量Django,python,django,django-views,Python,Django,Django Views,我正在尝试更新购物车上的数量,但它实际上没有更新数量。我的购物车基于模型,但我认为问题在于: product_obj['quantity'] += 1 我得到一个错误: “产品”对象不可下标 我的观点 def cart_update(request): product_id = request.POST.get('product_id') if product_id is not None: try: product_obj = Pro
product_obj['quantity'] += 1
我得到一个错误:
“产品”对象不可下标
我的观点
def cart_update(request):
product_id = request.POST.get('product_id')
if product_id is not None:
try:
product_obj = Product.objects.get(id=product_id)
except Product.DoesNotExist:
print("Show message to user, product is gone?")
return redirect("cart:home")
cart_obj, new_obj = Cart.objects.new_or_get(request)
if product_obj in cart_obj.products.all():
cart_obj.products.add(product_obj)
product_obj['quantity'] += 1
return redirect("cart:home")
您是否会使用(假设product_obj不是实际的列表或字典)
我们需要在更新值后保存对象。添加
产品对象保存()
。
product_obj.quantity += 1