Frederico Leonardo
Frederico Leonardo

Reputation: 95

django: UnboundLocalError

I have these functions on my 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)
    (...)

When I try to load the page that shows the cart, I get this error:

UnboundLocalError at /cart/
local variable 'cart_subtotal' referenced before assignment

I don't understand why I keep getting this error. The 'cart_items' variable works perfectly. I've been googling but can't seem to find an answer. Is it something really obvious?

Upvotes: 1

Views: 762

Answers (1)

gladysbixly
gladysbixly

Reputation: 2659

Why do you have this? One is a method and another is a variable.

cart_subtotal = cart_subtotal(request)

Upvotes: 2

Related Questions