遍历嵌套的 FormField
Iterating through a nested FormField
我正在尝试遍历 FormField 中的 FormField,它们都是 FieldList 的一部分。
在我的 views.py
中,我正在调用 mainForm
,模板通过 FormField subForm
成功迭代。但是,当我无法通过 subSubForm
进行迭代时。这些字段永远不会出现在浏览器中。
表格代码:
class subSubForm(Form):
step = IntegerField("step", validators=[NumberRange(min=0, max=99)])
description = TextField("Description")
information = TextAreaField("Information Exchanged")])
class subForm(Form):
name = TextField("Description")
step = FieldList(FormField(subSubForm), min_entries=1)
class mainForm(Form):
sub_form = FieldList(FormField(subForm), min_entries=1)
以及 Jinja2 模板:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in form.sub_form %}
{{ step.form.id(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
{% endfor %}
以上仅显示 sub_form.form.name
字段。
如何进行嵌套迭代以便同时显示 step
字段?
您的模板中的第二个嵌套循环似乎有误。这是它的固定版本:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in sub_form.step %}
{{ step.form.step(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
这呈现为:
<input id="sub_form-0-name" name="sub_form-0-name" placeholder="Scenario Title" type="text" value="">
<input id="sub_form-0-step-0-step" name="sub_form-0-step-0-step" placeholder="#" type="text" value="">
<input id="sub_form-0-step-0-description" name="sub_form-0-step-0-description" placeholder="description" type="text" value="">
<textarea id="sub_form-0-step-0-information" name="sub_form-0-step-0-information" placeholder="info xch"></textarea>
我正在尝试遍历 FormField 中的 FormField,它们都是 FieldList 的一部分。
在我的 views.py
中,我正在调用 mainForm
,模板通过 FormField subForm
成功迭代。但是,当我无法通过 subSubForm
进行迭代时。这些字段永远不会出现在浏览器中。
表格代码:
class subSubForm(Form):
step = IntegerField("step", validators=[NumberRange(min=0, max=99)])
description = TextField("Description")
information = TextAreaField("Information Exchanged")])
class subForm(Form):
name = TextField("Description")
step = FieldList(FormField(subSubForm), min_entries=1)
class mainForm(Form):
sub_form = FieldList(FormField(subForm), min_entries=1)
以及 Jinja2 模板:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in form.sub_form %}
{{ step.form.id(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
{% endfor %}
以上仅显示 sub_form.form.name
字段。
如何进行嵌套迭代以便同时显示 step
字段?
您的模板中的第二个嵌套循环似乎有误。这是它的固定版本:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in sub_form.step %}
{{ step.form.step(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
这呈现为:
<input id="sub_form-0-name" name="sub_form-0-name" placeholder="Scenario Title" type="text" value="">
<input id="sub_form-0-step-0-step" name="sub_form-0-step-0-step" placeholder="#" type="text" value="">
<input id="sub_form-0-step-0-description" name="sub_form-0-step-0-description" placeholder="description" type="text" value="">
<textarea id="sub_form-0-step-0-information" name="sub_form-0-step-0-information" placeholder="info xch"></textarea>