当我向服务器发送get请求时,POST方法也会被执行并给我一个错误,即bookForm变量尚未初始化。我的问题是当我点击GET请求时,POST方法代码永远不会被执行!另外我如何解决这个问题。 django打印时出错:在赋值之前引用的局部变量'bookForm'(在第一次引用它的行...这里是bookForm.isValid()调用。)
def main(request):
if request.method == 'POST':
bookForm = BookForm(request.POST)
# check whether it's valid:
if bookForm.is_valid():
# process the data in form.cleaned_data as required
return HttpResponse('<h1>Page was found</h1>')
else:
bookForm = BookForm()
return render(request,'home.html',{'bookForm':bookForm,})