3

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>
5
  • How are you defining the form? Commented Nov 5, 2012 at 17:35
  • Your code looks fine- there must be something else going on. Try putting print request.POST at the top of your view to see what's happening. Commented Nov 5, 2012 at 17:39
  • @dgel I put print request.POST in top of def contact, but nothing happens Commented Nov 5, 2012 at 17:44
  • It should print output onto the console running ./manage.py runserver. Commented Nov 5, 2012 at 17:45
  • @dgel <QueryDict: {u'csrfmiddlewaretoken': [u'XgBK59PvpAcCHDZQejjZiFtgkmeEDoQP'], u'sender': [u''], u'message': [u''], u'title': [u'']}> Commented Nov 5, 2012 at 17:51

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

:-O Respect! I've never encountered such an error. Guess I've never mixed tabs and spaces.
It happens, especially when copy and pasting from django docs (just the chance that was done was a reason I was pretty certain, beyond the main indicator that the code looks fine.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.