Django:查看"没有返回HttpResponse对象"

Apa*_*ear 0 python django

我在提交表单时没有返回httpresponse对象时收到以下错误,我无法理解为什么:

Exception Type: ValueError at /contact/addcontact
Exception Value: The view openshift.contactapplication.views.contact didn't return an HttpResponse object.
Run Code Online (Sandbox Code Playgroud)

这是我的观点:

# 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',)
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?我显然已经回来了HttpResponseRedirect

Rem*_*ich 7

您正在返回HttpResponseRedirectif方法是POST并且表单有效.如果method不是POST,则返回结果render(),即HttpResponse.到现在为止还挺好.

但是如果方法是POST并且表单无效,那么您不会显式返回任何内容(因此None返回).

这假设我的缩进是正确的 - 你的问题有点不对,例如下面的代码def没有缩进.