django - form has no errors but form.is_valid() doesn't validate
35,553
Solution 1
From your template, add the following:
{{ form.errors }}
{{ form.non_field_errors }}
Do you see any errors from the above?
Solution 2
You can also print (or log) error at back end too, Similarly use this:
Like, to print error:
print(form.errors)
Solution 3
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 %}
Author by
avatar
“Confidence in nonsense is required” - Burt Rutan "Better real confusion than false clarity" - Fr. Thomas Hopko
Updated on December 22, 2020Comments
-
avatar over 3 years
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()