【发布时间】:2018-08-20 13:29:01
【问题描述】:
我想在 django 表单字段的 help_text 中有一个 <ul>。
不幸的是,django 将 help_text 呈现在 <span> 中。
根据 HTML 规范,<span> 不得包含 <ul>。至少我的验证工具是这么说的。
这里是django的源码:https://github.com/django/django/blob/master/django/forms/forms.py#L283
def as_table(self):
"Return this form rendered as HTML <tr>s -- excluding the <table></table>."
return self._html_output(
normal_row='<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
error_row='<tr><td colspan="2">%s</td></tr>',
row_ender='</td></tr>',
help_text_html='<br><span class="helptext">%s</span>',
errors_on_separate_row=False)
我该怎么做才能在help_text 和有效的html中获得<ul>。
覆盖as_table() 不起作用,因为表单来自“core_app”而字段来自插件。两者都是两个不同的 git repos,我不想仅仅因为这个而修改核心。
【问题讨论】:
-
我认为覆盖
as_table会有所帮助(对于您想要的特定形式)。 -
或者,根本不要使用
as_table,并以您想要的格式单独输出您的字段。 -
@itzMEonTV 覆盖 as_table() 不起作用。我更新了问题。感谢您的反馈。
-
@DanielRoseman 我更新了问题。表单来自核心,help_text 来自插件......
-
我不知道这对我的评论有何影响。如文档中所述,
as_table纯粹是一种方便的方法;如果输出格式不适合您的需要,请不要使用。
标签: html django django-forms