django:form.fields 不遍历实例字段 [英] django: form.fields not iterating through instance fields
问题描述
我正在尝试遍历模板中的 form.fields 并用于:
{% for field in form.fields %}{{ 场地 }},{% 结束为 %}
我得到了一个字段名称列表(名称、描述..."),而不是使用以下内容时呈现的 html 代码:
{{ form.name }}, {{ form.description }}
(在这种情况下的输出是:
<input id="id_name" type="text" name="name" maxlength="200"/><input id="id_description"....>
有什么提示吗?谢谢!
您想要迭代表单",而不是表单.字段".后者返回 Field 实例,前者返回 BoundField 实例(即使在未绑定表单的情况下),它们呈现其小部件 HTML.
form.visible_fields 和 form.hidden_fields 是仅获取表单可见/隐藏字段的实用方法,但它们也返回 BoundField 实例.它们与 form.fields 完全不同(我同意这不是最清晰的 API).
I am trying to iterate through form.fields in a template and for:
{% for field in form.fields %}
{{ field }},
{% endfor %}
I am getting a list of the field names ("name, description...") instead of the html code that is rendered when using the following:
{{ form.name }}, {{ form.description }}
(the output in this case is:
<input id="id_name" type="text" name="name" maxlength="200" /><input id="id_description"....
Any hints? Thanks!
You want to iterate over "form," not "form.fields". The latter returns Field instances, the former returns BoundField instances (even in the case of an unbound form), which render their widget HTML.
form.visible_fields and form.hidden_fields are utility methods to only get the visible/hidden fields of the form, but they also return BoundField instances. They are not in any way parallel to form.fields (I agree that this isn't the clearest possible API).
这篇关于django:form.fields 不遍历实例字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!