Customize Django form validation form html output

Whenever you use the {{form.field.errors}} tag in a Django template, the displayed validation message is always surrounded by an unordered list tag. This is not perfect for me. Can I change the surrounding validation html message for a form from a reusable package?

+6
django django-forms
source share
1 answer

From django docs about looping through form fields :

{{ field.errors }}
The output is a <ul class="errorlist"> containing any validation errors corresponding to this field. You can customize the presentation of the error with the loop {% for error in field.errors %} . In this case, each object in the loop is a simple string containing an error message.

So, for example, to wrap every error in the <p> tags, you would do:

 {% for error in field.errors %} <p>{{ error|escape }}</p> {% endfor %} 
+8
source share

All Articles