我在views.py上有这些功能:
def get_cart_items(request):
return CartItem.objects.filter(cart_id=_cart_id(request))
def cart_subtotal(request):
cart_total = decimal.Decimal('0.00')
cart_products = get_cart_items(request)
for cart_item in cart_products:
cart_total += cart_item.product.price * cart_item.quantity
return cart_total
def show_cart(request):
cart_items = get_cart_items(request)
cart_subtotal = cart_subtotal(request)
(...)
当我尝试加载显示购物车的页面时,出现此错误:
UnboundLocalError at /cart/
local variable 'cart_subtotal' referenced before assignment
我不明白为什么我一直收到这个错误。 'cart_items'变量非常有效。我一直在谷歌搜索,但似乎无法找到答案。它真的很明显吗?
答案 0 :(得分:2)
你为什么这么做?一个是方法,另一个是变量。
cart_subtotal = cart_subtotal(request)