I am using django to set up the cart functionality on my ecommerce site. All items are entered as cart_item
s in a MySQL table.
Before the question, the relevant code:
charm = False
if postdata.get('charm_sku'):
charm_sku = postdata.get('charm_sku','')
ch = get_object_or_404(Charm, sku=charm_sku)
#get products in cart
cart_products = get_cart_items(request)
product_in_cart = False
# check to see if item is already in cart
for cart_item in cart_products:
if cart_item.product.id == p.id:
if charm == True:
if cart_item.charm.id == ch.id:
# update the quantity if found
cart_item.augment_quantity(quantity)
product_in_cart = True
else:
if cart_item.charm.id == "":
# update the quantity if found
cart_item.augment_quantity(quantity)
product_in_cart = True
Edit:
I reworked the code as shown above, causing BOTH if cart_item.charm.id
's to throw the attirbuteerror: 'NoneType' object has no attribute 'id'
. In a way, I think this has improved the situation, since I suspect the first one seeming to "succeed" was in fact the first if charm == True
failing, thus never testing the first if cart_item.charm.id == ch.id
. The question remains: why is this trowing the AttributeError, when the For loop is clearly declaring the cart_item, and cart_items clearly have both a charm column and id's assigned to said columns?
Edit 2: Can I not reference cart_item from the nested if's? That is the only thing I can think, but at the same time I feel like I should be able to, so maybe that is wrong?
NoneType
means that instead of an instance of a class, you've actually got None
. That probably means that an assignment failed or or function call returned an unexpected result. Your assignment to cart_item
is probably failing in the case where charm == False
. Check whatever code (assignment or function call) that's setting those two variables.