Django自定义文件语法错误

时间:2017-07-26 11:34:04

标签: django

我在“cart”应用中为基本购物车功能创建的cart.py文件中出现语法错误

  

如果cart_item.menuitem.id = i.id:

cart.py:

# add an item to the cart
def add_to_cart(request):
    postdata = request.POST.copy()
    # get menuitem slug from post data, return blank if empty
    menuitem_slug = postdata.get('menuitem_slug','')
    # get quantity added, return 1 if empty
    quantity = postdata.get('quantity',1)
    # fetch the menuitem or return a missing page error
    i = get_object_or_404(MenuItem, slug=menuitem_slug)
    #get menuitems in cart
    cart_menuitems = get_cart_items(request)
    menuitem_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_menuitems:
        if cart_item.menuitem.id = i.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.menuitem = i
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save() 

# returns the total number of items in the user's cart
def cart_distinct_item_count(request):
    return get_cart_items(request).count() 

1 个答案:

答案 0 :(得分:2)

你正在分配一个值吗?或检查??

更改此行

2

if cart_item.menuitem.id = i.id: