django - 自定义 Django 表单验证的 html 输出

标签 django django-forms

每当您在 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/

相关文章:

PostgreSQL 的 Django 错误

python - Django:查询集未保存对值的更改?

python - 在 Web 服务器上运行时,Paramiko SSH 失败并显示 "Server ' .. .' not found in known_hosts"

python - 使用 HTML 模板的 Django 表单

Django ajax 表单,选择字段依赖

python - 如何获得Python的等价物[:-1] in django ORM?

python - 使用 django-sslserver 从 Django View 返回图像

ModelFormset 的 Django 删除按钮操作

django queryset - 搜索名字和姓氏

django - 模型 unique_together 约束 + None = 失败?