Skip to content
Advertisement

Django / Python: TypeError at / ‘NoneType’ object is not subscriptable

Errors showing in views.py at line data = cartData(request) and utils.py cartItems = cookieData['cartItems']

Errors showing exactly:

Exception Location: C:xampphtdocsecommercestoreutils.py, line 55, in cartData
C:xampphtdocsecommercestoreviews.py, line 12, in store
    data = cartData(request) 
C:xampphtdocsecommercestoreutils.py, line 55, in cartData
cartItems = cookieData['cartItems'] 

views.py

from . utils import cookieCart, cartData
    
def store(request):
    data = cartData(request)
    cartItems = data['cartItems']
    order = data['order']
    items = data['items']

    products = Product.objects.all()
    context = {'products': products, 'cartItems': cartItems}
    return render(request, 'store/store.html', context)

utils.py

def cartData(request):
if request.user.is_authenticated:
    customer = request.user.customer
    order, created = Order.objects.get_or_create(
        customer=customer, complete=False)
    items = order.orderitem_set.all()
    cartItems = order.get_cart_items
else:
    cookieData = cookieCart(request)
    cartItems = cookieData['cartItems']
    order = cookieData['order']
    items = cookieData['items']
    return {'cartItems': cartItems, 'order': order, 'items': items}

Advertisement

Answer

Your return statement in cartData() function should not be inside the else block. It should be in level same as your if else.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement