class PersonForm(Form):
name = forms.CharField()
email = forms.EmailField()
age = forms.IntegerField()
# And here's how you would output its fields using a custom template:
{% for field in form %}
<div>
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
# By default, {{ field.errors }} outputs a <ul class="errorlist"> containing
# the validation errors, or a blank string if there are none. If you want to
# further customise the way in which errors are displayed, you can do this:
{% for field in form %}
<div>
{% if field.errors %}
{% for error in field.errors %}
<p style="color: red">Oh no! {{ error }}</p>
{% endfor %}
{% endif %}
{{ field.label_tag }}: {{ field.as_html }}
</div>
{% endfor %}
Comments