Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying django form error messages instead of just the field name

I have a form and I want to display the errors in a for loop.

{% for error in form.errors %}
    <tr><td>{{ error }}</td></tr>
{% endfor %}

By doing this the {{ error }} only contains the field name that has an error, but not the error message. How can I display the error message?

like image 437
bash- Avatar asked Oct 14 '25 15:10

bash-


1 Answers

You can get all field errors in a form like this:

{% for field in form %}
  {{ field.errors|striptags }}
{% endfor %}

Or for a specific field:

{% if form.subject.errors %}
    <ol>
    {% for error in form.subject.errors %}
        <li><strong>{{ error|escape }}</strong></li>
    {% endfor %}
    </ol>
{% endif %}

More Infos here: https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

like image 173
arie Avatar answered Oct 17 '25 06:10

arie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!