我收到以下错误,因为在提交表单时没有返回httpresponse对象,我真的不明白为什么:
Exception Type: ValueError at /contact/addcontact
Exception Value: The view openshift.contactapplication.views.contact didn't return an HttpResponse object.
这是我的观点:
# Create your views here.
from django.shortcuts import render_to_response, render
from django.http import HttpResponseRedirect, HttpResponse
#forms imports
from contactapplication.forms import applicantsForm
#model imports
from contactapplication.models import applicantsModel
def contact(request):
if request.method == 'POST':
form = applicantsForm(request.POST)
if form.is_valid():
clean_form =form.cleaned_data
collect = applicantsModel(first_name=clean_form['first_name'], last_name=clean_form['last_name'],
linkedin_profile=clean_form['linkedin_profile'], elevator_pitch=clean_form['elevator_pitch'],
email=clean_form['email'], phone=clean_form['phone'])
collect.save()
return HttpResponseRedirect('contactUs/contactUs.html') #the same page, change to thankyou page later
else:
return render(request, 'contactUs/contactUs.html',)
可能是什么问题?我清楚地返回了一个HttpResponseRedirect
如果方法是POST并且表单有效,则返回HttpResponseRedirect
。若方法不是POST,则返回render()
的结果,这是一个HttpResponse。到目前为止还不错。
但是,如果方法是POST,并且表单无效,则不显式返回任何内容(因此返回None
)。
这是假设我的缩进是正确的——在你的问题中有点错误,例如def
下的代码没有缩进。