2013-03-27 211 views
0
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正在捕捉語法錯誤。Python/Django「if語句」語法不正確?

爲什麼點語法打破了if語句?如果需要更多的上下文,請告訴我,但我猜測我只是沒有正確理解if語句的語法要求。

+0

什麼是您收到的錯誤的確切文本? – bdesham 2013-03-27 16:50:30

+0

看起來像你需要== ==平等而不是'='賦值在這裏:'if cart_item.product.id = p.id:' – 2013-03-27 16:51:26

+0

'SyntaxError at/ 無效語法(cart.py,line 42)顯然,行'if ** cart_item。** product.id = p.id:'是行42 – fildred13 2013-03-27 16:51:41

回答

5

Python中的比較運算符是==而不是=