每当您在 Django 模板中使用 {{ form.field.errors }} 标签时,显示的验证消息总是被一个无序列表标签包围。这对我来说并不理想。我可以修改可重用包中表单的周围验证消息 html 吗?
最佳答案
来自关于 looping over a form's fields 的 Django 文档:
{{ field.errors }}
Outputs a<ul class="errorlist">
containing any validation errors corresponding to this field. You can customize the presentation of the errors with a{% for error in field.errors %}
loop. In this case, each object in the loop is a simple string containing the error message.
例如,将每个错误包装在
<p>
中你会做的标签:{% for error in field.errors %}
<p>{{ error|escape }}</p>
{% endfor %}
关于django - 自定义 Django 表单验证的 html 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1745851/