How to Display Form Errors in Template in Django
In Django templates, display form errors using the
form.errors attribute or by iterating over form.field.errors for specific fields. Use template tags like {% if form.errors %} to conditionally show error messages near the form inputs.Syntax
To display errors for the whole form, use form.errors. For field-specific errors, use form.field_name.errors. Wrap these in template tags to show errors only when they exist.
form.errors: Dictionary of all form errors.form.field_name.errors: List of errors for a specific field.{% if form.errors %}: Template condition to check if any errors exist.
django
{% if form.errors %}
<ul>
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error }}</li>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}Example
This example shows a Django form with a required field. When the form is submitted empty, errors display next to the input and at the top.
python/django
from django import forms from django.shortcuts import render class ContactForm(forms.Form): name = forms.CharField(required=True, max_length=100) # views.py def contact_view(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): # process form data pass else: form = ContactForm() return render(request, 'contact.html', {'form': form}) # contact.html <form method="post"> {% csrf_token %} {% if form.errors %} <div class="error-summary"> <p>Please fix the errors below:</p> <ul> {% for field in form %} {% for error in field.errors %} <li>{{ field.label }}: {{ error }}</li> {% endfor %} {% endfor %} {% for error in form.non_field_errors %} <li>{{ error }}</li> {% endfor %} </ul> </div> {% endif %} <label for="id_name">Name:</label> {{ form.name }} {% if form.name.errors %} <div class="field-error"> {% for error in form.name.errors %} <p>{{ error }}</p> {% endfor %} </div> {% endif %} <button type="submit">Send</button> </form>
Output
<form method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<div class="error-summary">
<p>Please fix the errors below:</p>
<ul>
<li>Name: This field is required.</li>
</ul>
</div>
<label for="id_name">Name:</label>
<input type="text" name="name" required maxlength="100" id="id_name">
<div class="field-error">
<p>This field is required.</p>
</div>
<button type="submit">Send</button>
</form>
Common Pitfalls
Common mistakes when displaying form errors include:
- Not checking if errors exist before displaying, causing empty error blocks.
- Displaying
form.errorswithout iterating, which shows raw data not user-friendly messages. - Forgetting to include
{% csrf_token %}in POST forms, leading to form submission failure. - Not handling
non_field_errorswhich are errors not tied to a specific field.
django
{# Wrong: showing raw errors without check #}
{{ form.errors }}
{# Right: check and iterate errors #}
{% if form.errors %}
<ul>
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error }}</li>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}Quick Reference
| Template Expression | Description |
|---|---|
| form.errors | All errors in the form as a dictionary |
| form.field_name.errors | List of errors for a specific field |
| form.non_field_errors | Errors not related to any field |
| {% if form.errors %}...{% endif %} | Conditionally show errors if any exist |
| {% for error in form.field_name.errors %}{{ error }}{% endfor %} | Loop through and display field errors |
Key Takeaways
Use
form.errors and form.field_name.errors to access errors in templates.Always check if errors exist with
{% if form.errors %} before displaying them.Display
non_field_errors to show errors not tied to specific fields.Include
{% csrf_token %} in POST forms to avoid submission errors.Iterate over errors to show user-friendly messages instead of raw error data.