38

I have a form that for some reason it it doesn't throw any errors (if user adds data in the text field) but form.is_valid() doesn't validate. Any ideas?

forms.py:

class MyForm(forms.Form):
    title = forms.CharField(widget=forms.TextInput(attrs={'class':'titleField')) 

mytemplate.html

  <form action="" method="post" name="form">{% csrf_token %}
  {{ form.title.errors }}
  {{ form.title }}
  <input type="submit" name='submit_button' value="Post" />
  </form>

views.py:

      if 'submit_button' in request.POST:
            form = MyForm(request.POST)
            if form.is_valid():
               cd = form.cleaned_data 
               title = cd['title']
               g = MyData(title='title')   
               g.save()

       else:
            form = MyForm() 

3 Answers 3

60

From your template, add the following:

{{ form.errors }}
{{ form.non_field_errors }}

Do you see any errors from the above?

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

4 Comments

{{ form.errors }} {{ form.non_field_errors }} doesn't shown any errors for me :( but still its not updating . form is a bound one , instance also passed to the form
If the two above don't work, then you are not passing the GET or POST parameter to the form, hence its not getting any data and not validating. Be sure the form you are calling was passed a GET or POST from request.
I had the same problem and in the end I tracked it down to a boolean field that I had mistakenly set to forms.ChoicesField not forms.BoooleanField in forms.py. So make sure all your field types are correct because for me the error was silent.
when I was subclassing an AuthenticationForm, I was trying to override some widget attributes, but in the constructor, while doing super().__init__(self, *args, **kwargs) I forgot to pass self and hence I was not able to see any errors. After fixing this I was able to see the errors. so pls check your forms.py file for any silly mistakes
17

You can also print (or log) error at back end too, Similarly use this:

Like, to print error:

print(form.errors)

1 Comment

Not always possible when you have Sentry or similar installed
1

I'm using this block with bootstrap

{% if form.errors %}
<div class="alert alert-danger" role="alert">
    {% for field, errors in form.errors.items %}
    {% for error in errors %}
    <b>{{ field }}</b>: {{ error }}
    {% endfor %}
    {% endfor %}
</div>
{% endif %}

Comments

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.