gpt4 book ai didi

python - 迭代嵌套的 FormField

转载 作者:行者123 更新时间:2023-12-01 04:51:41 26 4
gpt4 key购买 nike

我正在尝试迭代 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>

关于python - 迭代嵌套的 FormField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com