Python / Django“if语句”语法不正确?

时间:2013-03-27 16:48:46

标签: python django if-statement syntax-error

for cart_item in cart_products:
    if **cart_item.**product.id = p.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.product = p
    ci.quantity = quantity
    ci.cart_id = _cart_id(request)
    ci.save()

我刚刚将我的购物车放在一起,这是一个简单的逻辑,检查以确保该项目不在某人的购物车中,以防他们再次尝试再次添加它。错误在粗体部分中出现...预期:而不是.服务器抛出错误并且eclipse正在捕获语法错误。

为什么点语法会破坏if语句?如果需要更多上下文,请告诉我,但我猜我只是没有正确理解if语句的语法要求。

1 个答案:

答案 0 :(得分:5)

Python中的比较运算符是==,而不是=