【已解决】local variable 'form' referenced before assignment
程序员文章站
2024-02-17 14:03:22
...
制作表单实例的时候报错.
我的views.py文件如下:
# test 测试页面的传递数据
def test(request):
if request.method == 'POST': # 当提交表单的时候
form = AddForm(request.POST) # form包含提交的数据
if form.is_valid(): # 如果提交的数据合法
a = form.cleaned_data['a']
b = form.cleaned_data['b']
return HttpResponse(str(int(a) + int(b)))
else: # 当正常访问时
form = AddForm()
return render(request, "test.html", {'form': form})
错误翻译过来就是,请在调用form之前得声明,意思就是还没声明....可是我已经声明过了.这是为啥
间距!!!
def test(request):
if request.method == 'POST': # 当提交表单的时候
form = AddForm(request.POST) # form包含提交的数据
if form.is_valid(): # 如果提交的数据合法
a = form.cleaned_data['a']
b = form.cleaned_data['b']
return HttpResponse(str(int(a) + int(b)))
else: # 当正常访问时
form = AddForm()
return render(request, "test.html", {'form': form})
即可正常访问!
所以说对于Python来说格式是十分重要的- -
上一篇: php创建xml并格式化输出
推荐阅读
-
【已解决】local variable 'form' referenced before assignment
-
python文件运行报错:UnboundLocalError: local variable 'status' referenced before assignment
-
pycharm 报黄(黄字、黄色)警告 Local variable 'xxx' might be referenced before assignment
-
每日BUG:UnboundLocalError: local variable ‘X‘ referenced before assignment
-
Python | local variable 'xxxx' referenced before assignment
-
_markupbase.py if not match: UnboundLocalError: local variable 'match' referenced before assignment,分析Python 库 html.parser 中存在的一个解析BUG
-
【ROS-Error】 Can't convert image: local variable ‘pil_mode‘ referenced before assignment
-
Python | local variable 'xxxx' referenced before assignment
-
Python:UnboundLocalError: local variable 'num' referenced before assignment
-
【ROS-Error】 Can't convert image: local variable ‘pil_mode‘ referenced before assignment