Reputation: 21
Hey guys I am running a blog with Django on a server but the contact form is not working, and after messing with it a bit, I broke it but the code is the original so I have no idea why it wouldn't be working. Here is my contact form:
def contact(request):
errors = []
if request.method == 'POST':
if not request.POST.get('subject', ''):
errors.append('Enter a subject.')
if not request.POST.get('message', ''):
errors.append('Enter a message.')
if request.POST.get('email') and '@' not in request.POST['email']:
errors.append('Enter a valid e-mail address.')
if not errors:
send_mail(
request.POST['subject'],
"User's Email: " + request.POST['email'] + " User's Message: " + request.POST['message'],
request.POST.get('email', '[email protected]'),
['[email protected]'], #email address where message is sent.
)
return HttpResponseRedirect('/thanks/')
return render(request, 'contact.html',
{'errors': errors})
Now I get this error when I run this page in the site:
ValueError at /contact/
The view blog.views.contact didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Any help would be appreciated.
Upvotes: 0
Views: 351
Reputation: 21
So I did what Peter suggested, checking the indentation of the last return statement (needed to not be indented) and yes... that was it. It's almost like I knew I was going to overlook something like this :p thanks to Peter and everybody for your help, I really appreciate you guys!! Have a good weekend
Upvotes: 1
Reputation: 230
If you want to redirect at other view, I advice you to use :
return redirect(<name of a view>, permanent=True)
instead of HttpResponseRedirecter
Upvotes: 1
Reputation: 5306
can you try
return render_to_response(request, 'contact.html',
{'errors': errors})
Upvotes: 1