[英]Django: View “didn't return an HttpResponse object”
I am recieving the following error for not returning an httpresponse object when submitting a form, and I cannot really understand why: 我收到以下错误,因为在提交表单时未返回httpresponse对象,我无法真正理解为什么:
Exception Type: ValueError at /contact/addcontact
Exception Value: The view openshift.contactapplication.views.contact didn't return an HttpResponse object.
Here is my view: 这是我的看法:
# 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',)
What could be the issue? 可能是什么问题? I am clearly returning an
HttpResponseRedirect
我显然返回了
HttpResponseRedirect
You are returning a HttpResponseRedirect
if method is POST and the form is valid. 如果方法是POST并且表单有效,则返回
HttpResponseRedirect
。 If method isn't POST, you return the result of render()
, which is a HttpResponse. 如果method不是POST,则返回
render()
的结果,它是HttpResponse。 So far so good. 到现在为止还挺好。
But if method is POST and the form isn't valid , then you don't return anything explicitly (so None
is returned). 但是,如果method为POST且表单无效 ,则您不会显式返回任何内容(因此将返回
None
)。
This is assuming I got the indentation right -- it's a bit wrong in your question, eg the code under the def
isn't indented. 这是假定我正确的缩进-您的问题有点错,例如
def
下的代码未缩进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.