Inside of an if statement I've check_order that I need to have as a context variable for my template, I'm getting this traceback: local variable 'check_order' referenced before assignment. How do I have it as a context variable without having to repeat the code to have it outside of the if statement?
View
if request.method == "POST":
if request.user.is_authenticated:
customer = request.user.customer
check_order = OrderItem.objects.filter(order__customer=customer)
if check_order:
if form.is_valid():
#does logic
else:
messages.error(request, f"Failed")
else:
return redirect()
context = {"check_order": check_order}
check_orderbefore your if statement otherwise check if thecheck_orderquery returns a queryset, if it returns none, thats your problem