【发布时间】:2020-12-01 04:11:20
【问题描述】:
在我的 Django 应用程序中使用 views.py 出现此错误 - UnboundLocalError 在 / 赋值前引用的局部变量“上下文”。
这里是一个不工作的代码的 sn-p:
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)
【问题讨论】:
-
context在您引用它之前未在任何地方定义,正如错误消息所述。我相信你犯了一个简单的错字,context: {'products':products}->context = {'products':products}否则你所拥有的是一个变量 type annotation 而不是赋值语句 -
这是错字,谢谢指出。现在效果很好。
标签: python django django-views