表单上 django 非字段错误的样式
如果我查看 Django 的 forms.py,as_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用此模板来设置非字段错误的样式:
I use this template to style non fields errors:
在您的模板中,您可以访问 {{ 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
选项:
{{ form.non_field_errors }}
和{{ form.non_field_errors.as_ul }}
执行相同的操作。{{ form.non_field_errors.as_text }}
在文本前面显示带有*
的错误。本文还有助于解释为什么
*
不会被删除django TicketOptions:
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无需使模板语言复杂化即可完成。在 Django 3 中,non_field_errors 列表具有“errorlist nonfield”类,因此您可以简单地使用 CSS 来设置它们的样式:
在 CSS 文件中:
在 html 模板中:
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:
In your html template: