2012-07-03 166 views
0

我有一個非常基本的視圖,我正在保存表單。出於某種原因,我不斷收到「視圖沒有返回HttpResponse對象」錯誤。我在這裏看到了這個常見問題,但還沒有找到適用於我的解決方案。有人有想法嗎?我已經包含下面的代碼。非常感謝幫助這個簡單的問題!Django「視圖沒有返回HttpResponse對象。」

def EarlyAdopterSignup(request): 
    f = LandingPageForm(request.POST) 
    if f.is_valid(): 
     email = f.cleaned_data['email'] 
     zip = f.cleaned_data['zip'] 
     adopter = EarlyAdopter(email = email, zip = zip) 
     try: 
      adopter.save() 
      return render_to_response('EarlyAdopterSignup.html') 
     except: 
      return HttpResponse("There was an error with your submission. Please try again.") 

回答

3

那麼,首先,如果您的表單無效,您不會返回任何內容。您應該重新顯示顯示錯誤的表單。

5

爲了跟進A.L的說法,有必要處理表格無效的情況。

通過將綁定的無效表單(f)傳遞迴您的模板,可以很簡單地處理此問題。

https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#using-a-form-in-a-view

是你需要做的^

def EarlyAdopterSignup(request): 
    f = LandingPageForm(request.POST) 
    if f.is_valid(): 
     email = f.cleaned_data['email'] 
     zip = f.cleaned_data['zip'] 
     adopter = EarlyAdopter(email = email, zip = zip) 
     try: 
      adopter.save() 
      return render_to_response('EarlyAdopterSignup.html') 
     except: 
      return HttpResponse("There was an error with your submission. Please try again.") 

    # handle where not valid 
    return render(request, 'your_form_template.html', { 
    'form': f, # <- your invalid form instance 
}) 
正是一個例證
相關問題