I have this in my template and i want only to display username error not all form errors
{% for field in form %}
{% for error in field.errors %}
<div class="row">
<div class="small-12 columns val-error-msg error margin-below">
{{ error }}
</div>
</div>
{% endfor %}
{% endfor %}
You could specify a field error with form.field_name.errors
like:
{% if form.username.errors %}
{{form.username.errors}}
# or
# {{form.username.errors.as_text}}
# this will remove the `<ul></ul>` tag that django generates by default
{% endif %}
Ever field has .errors
attached to it as well. But note that each field can contain multiple errors (for example a password can be too short, and contain illegal symbols).
You can access these errors through {{ form.field.errors }}
, but you already obtain such elements. In case you want to filter in the template to only show the errors of - for example - the username
field, you can do so with an {% if ... %}
statement:
{% for field in form %}
{% if field.name == "username" %}
{% for error in field.errors %}
<div class="row">
<div class="small-12 columns val-error-msg error margin-below">
{{ error }}
</div>
</div>
{% endfor %}
{% endif %}
{% endfor %}
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