2014-04-18 38 views
0

我recieving以下錯誤提交表單時不返回一個HttpResponse對象,我真的不能明白爲什麼:Django的:查看「沒有返回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

回答

7

如果方法是POST並且表單有效,則返回HttpResponseRedirect。如果method不是POST,則返回結果render(),這是一個HttpResponse。到現在爲止還挺好。

但是,如果方法是POST並且表單無效,那麼您不明確返回任何內容(因此返回None)。

這是假設我得到了縮進權 - 在你的問題上有點錯誤,例如, def下的代碼不縮進。

+0

感謝您指出,非常感謝。是的,你閱讀正確。 – ApathyBear

相關問題