django:form.fields不迭代实例字段 [英] django: form.fields not iterating through instance fields

查看:419
本文介绍了django:form.fields不迭代实例字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在模板中迭代form.fields,并为:

I am trying to iterate through form.fields in a template and for:

{% for field in form.fields %}
   {{ field }}, 
{% endfor %}

我收到一个列表的字段名称(name,description ...),而不是使用以下代码时呈现的html代码:

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_nametype =textname =namemaxlength =200/ < input id =id_description....

任何提示?
谢谢!

Any hints? Thanks!

推荐答案

你想迭代form而不是form.fields,后者返回Field实例,前者返回BoundField实例(即使在一个未绑定的表单),它呈现其小部件HTML。

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和form.hidden_​​fields是仅实现可见/窗体的隐藏字段,但它们也返回BoundField实例。他们不是以任何方式平行于form.fields(我同意这不是最清楚的API)。

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屋!

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