django: form.fields not iterating through instance fields

后端 未结 3 1163
梦如初夏
梦如初夏 2020-12-10 09:00

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

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

I am getting

3条回答
  •  一生所求
    2020-12-10 09:34

    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).

提交回复
热议问题