表单上 django 非字段错误的样式

发布于 2024-08-03 14:09:10 字数 309 浏览 8 评论 0原文

如果我查看 Django 的 forms.pyas_p() 调用 _html_output(),它使用 self.error_class( ) (虽然我找不到它的定义)。

但是,_html_output() 不会设置 non_field_errors 的样式(在代码中也称为 top_errors)。

如何设计非字段错误的样式?剪切并粘贴所有 _html_output?

我正在使用 Django 1.0。

If I look in Django's forms.py, as_p() calls _html_output(), which styles field errors with self.error_class() (although I can't locate the definition of that).

However, _html_output() does NOT style non_field_errors (a.k.a. top_errors in the code).

How does one style the non-field errors? Cut and paste all of _html_output?

I am using Django 1.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金橙橙 2024-08-10 14:09:10

我使用此模板来设置非字段错误的样式:

{% if form.non_field_errors %}
  <div class="non-field-errors">
    {% for err in form.non_field_errors %}
      <p class="form-error">{{ err }}</p>
    {% endfor %}
  </div>
{% endif %}

I use this template to style non fields errors:

{% if form.non_field_errors %}
  <div class="non-field-errors">
    {% for err in form.non_field_errors %}
      <p class="form-error">{{ err }}</p>
    {% endfor %}
  </div>
{% endif %}
自控 2024-08-10 14:09:10

在您的模板中,您可以访问 {{ form.non_field_errors }} 并按照您喜欢的方式呈现它们。以下是 Django 管理员处理它们的方式,例如: http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/admin/change_form.html#L40

In your template you can access {{ form.non_field_errors }} and render them however you like. Here's how the Django admin handles them for example: http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/admin/change_form.html#L40

泼猴你往哪里跑 2024-08-10 14:09:10

选项:

{{ form.non_field_errors }}{{ form.non_field_errors.as_ul }} 执行相同的操作。

{{ form.non_field_errors.as_text }} 在文本前面显示带有 * 的错误。

本文还有助于解释为什么 * 不会被删除django Ticket

Options:

Both {{ form.non_field_errors }} and {{ form.non_field_errors.as_ul }} do the same thing.

{{ form.non_field_errors.as_text }} displays the errors with an * in front of the text.

This article is also helpful in explaining why the * will not be removed django ticket

¢好甜 2024-08-10 14:09:10

无需使模板语言复杂化即可完成。在 Django 3 中,non_field_errors 列表具有“errorlist nonfield”类,因此您可以简单地使用 CSS 来设置它们的样式:

在 CSS 文件中:

.errorlist.nonfield {
    color: red;
}

在 html 模板中:

{{ form.as_p }}

Can be done without complicating the template language. In Django 3 the non_field_errors list has classes "errorlist nonfield", so you can simply use CSS to style them:

In your CSS file:

.errorlist.nonfield {
    color: red;
}

In your html template:

{{ form.as_p }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文