ApathyBear
ApathyBear

Reputation: 9595

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:

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

Upvotes: 0

Views: 1849

Answers (1)

RemcoGerlich
RemcoGerlich

Reputation: 31250

You are returning a HttpResponseRedirect if method is POST and the form is valid. If method isn't POST, you return the result of render(), which is a 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).

This is assuming I got the indentation right -- it's a bit wrong in your question, e.g. the code under the def isn't indented.

Upvotes: 7

Related Questions