我正在尝试创建新表单,在此过程中我遇到了以下问题:视图myapp.views.contact没有返回HttpResponse对象。 它返回None。请帮帮我
问题在于返回渲染函数,不知道如何解决此HttpResponse错误
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm,SnippetForm
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
name= form.cleaned_data['name']
email=form.cleaned_data['email']
print(name,email)
form = ContactForm()
return render(request,'form.html',{'form':form})
def snippet_detail(request):
if request.method == 'POST':
form = SnippetForm(request.POST)
if form.is_valid():
print("VALID")
form = SnippetForm()
return render(request,'form.html',{'form': form})
#form.save()
错误消息在浏览器中,问题本身就是错误消息。
答案 0 :(得分:2)
可能存在表格错误
您需要处理表单错误的情况。
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
name= form.cleaned_data['name']
email=form.cleaned_data['email']
print(name,email)
form = ContactForm()
return render(request,'form.html',{'form':form})
else:
return render(request,'form.html',{'form':form})
else:
form = ContactForm()
return render(request,'form.html',{'form':form})
希望有帮助。请参阅this