django表单错误。没有任何HTML标签的错误 [英] django form errors. get the error without any html tags

查看:62
本文介绍了django表单错误。没有任何HTML标签的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模板上获取非HTML版本的表单错误。

I would like to get on my template the errors of the form in non-html version.

默认情况下,错误由< ul class = errorlist> 我要避免。

by default the error is wrap up by <ul class="errorlist"> which I want to avoid.

是否可以在不进行大量代码更改的情况下执行此操作?

Anyway to do it without a massive code change ?

推荐答案

表单文档部分中的本部分具有所有详细信息,即此摘要:

This section in the forms documentation section has all the details, namely this snippet:

{% if form.subject.errors %}
    <ol>
    {% for error in form.subject.errors %}
        <li><strong>{{ error|escape }}</strong></li>
    {% endfor %}
    </ol>
{% endif %}

替换表格以及在模板中调用表单类的任何内容。在此示例中,主题是表单中的字段。如果您希望有一个单独的部分来汇总所有错误,只需遍历以下字段:

Replace form with whatever you call your form class in the template. In this example, subject is a field in the form. If you want to have a separate section to summarize all the errors, simply loop through the fields:

{% if form.errors %}
   {% for field in form %}
      {% for error in field.errors %}
          {{ error|escape }}
      {% endfor %}
   {% endfor %}
{% endif %}

这篇关于django表单错误。没有任何HTML标签的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆