I read this and have the bug in code - I can send "null" by the form and there is no error message, its just go to 'thanks' page. I can write 'nothing' to the email field, and result will be the same. How to fix it?
models:
class Contact(forms.Form):
title = forms.CharField(max_length=100)
message = forms.CharField(max_length=255)
sender = forms.EmailField()
views:
def contact(request):
if request.method == 'POST':
form = Contact(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
return HttpResponseRedirect('/thanks/')
else:
form = Contact()
return render(request, 'contact.html', {'form': form,})
template:
<form action="/contact/" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.title.errors }}
<label for="id_subject">Subject</label>
{{ form.title }}
{{ form.message.errors }}
<label for="id_message">Text</label>
{{ form.message }}
{{ form.sender.errors }}
<label for="id_sender">Email</label>
{{ form.sender }}
<p><input type="submit" value="Send" /></p>
</form>
I've got my money on you having mixed tabs and spaces. I haven't seen this in a while - it used to be pretty common!
It appears the return statement is firing no matter what on POST.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With