Reputation: 9479
I am trying to display a list of fields within a form, with any field that is not properly validated or filled out (if required) to display at the top of the page. I have the following code in my html body:
{% if form.errors %}
<b>Incorrect fields to correct:</b>
<ul>
{% for error in form.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
and this loops through all of my fields that have errors in it. However, the data that is displayed from
{{ error }}
are my variable names. Can I change my output so that I can display different output replacing my variables names?
My variables are within this class:
from django import forms
from django.core.exceptions import ValidationError
from django.core import validators
class InformationForm(forms.Form):
#...
full_name = forms.CharField(max_length=130)
#rest of fields follow...
Upvotes: 0
Views: 299
Reputation: 1386
In your form you can define custom error messages.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#error-messages
Something along the lines of:
class MyForm(ModelForm):
my_field=forms.CharField(... error_messages={'required': 'My Field',})
EDIT: Try the following:
{% for field in form%}
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
Upvotes: 2