pythondjangodjango-views

UnboundLocalError at / local variable 'context' referenced before assignment


Getting this error with views.py in my Django app - UnboundLocalError at / local variable 'context' referenced before assignment.

Here is a snippet of the code in my views.py that is not working:

from django.shortcuts import render
from .models import *

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

Solution

  • Try the following:

    from django.shortcuts import render
    from .models import *
    
    def store(request):
    products = Product.objects.all()
    context = {'products':products} #I have changed : to =
    return render(request, 'store/store.html', context)