好的,我已经在网上,文档和源代码中查看过了,而且我没有解决这个问题。所以我正在使用wtforms和bootstrap进行Flask项目,我需要在页面上多次显示相同的表单。具体来说,这种形式:
class ProblemForm(Form):
def __init__(self, formdata=None, obj=None, prefix='', title="", **kwargs):
super(self.__class__, self).__init__(formdata=None, obj=None, prefix='', **kwargs)
self.title = title
opts = RadioField("Possible Corrections")
alias_of = StringField("Alias of")
select_alias = BooleanField("select_alias")
user_input = StringField("New Entry")
select_uinput = BooleanField("select_uinput")
在控制器逻辑中(见下文)我发现我可以创建一个ProblemForm实例数组,为每个实例设置选项"问题"来自会议中已有的数据。
@blueprint.route("/clean/review/<int:start>-<int:end>", methods=["GET", "POST"])
@login_required
def cleaning(start, end):
prob_forms = []
for pif in xrange(start, end+1):
pf = ProblemForm(request.form, title=session["problems"][pif][0])
pf.opts.choices = [(pr, pr) for pr in session["problems"][pif][1]]+[('<null>', '<null>')]
prob_forms.append(pf)
if request.method == "POST":
pass
return render_template("main/cleaning.html", forms=prob_forms, buttons=ControlButtons(),
start=start, end=end)
令人沮丧的是,这个几乎有效。当表格发布时 request.form.lists()包含:[(&#39; select_uinput&#39;,[u&#39; 7&#39;]),(&#39; user_input&#39;,[u&#39;&#39; ;,你&#39;,你&#39;,你&#39;,&#39;&#39;,你&#39;&#39;,你&#39; ;&#39;,u&#39;]),(&#39; select_alias&#39;,[u&#39; 2&#39;]),(&#39; alias_of&#39;,[你&#39;,你和Mike Rowe的肮脏工作&#39;,你&#39;,你&#39;&#39;,你&#39;&#39;,你& #39;&#39;,u&#39;&#39;,&#39;&#39;]),(&#39;选择&#39;,[u&#39; Paradise&#39;]), (&#39; next&#39;,[u&#39; Next&#39;])]。我可以用这个;其中一个复选框是在第7个表单上选中的,另一个是在第二个表单上选中的,在用户键入的第二个表单上是“Mike Rowe&#39; Dirty Jobs”。在&#34; alias_of&#34;盒等等。
问题是RadioField名为&#34; opts&#34;。它似乎只保存用户按下的最后一个按钮。任何人都知道为什么会这样,或者我应该在哪里寻找更多信息?
这是模板,如果有任何用途:
{% extends "layout.html" %}
{% block content %}
<form action="{{ url_for('main.cleaning', start=start, end=end) }}" method="POST" enctype="multipart/form-data">
{% for form in forms %}
{% set outloop = loop %}
<div class="well well-lg">
<h3><em>{{ form.title }}</em>?</h3>
<!-- Possible Master List entries -->
<div class="btn-group" id="opts_{{outloop.index}}" role="group" data-toggle="buttons" aria-label="...">
{% for opt in form.opts %}
{% if loop.first %}
<label class="btn btn-default active">
{% else %}
<label class="btn btn-default">
{% endif %}
{{ opt }} {{ opt.label.text }}
</label>
{% endfor %}
</div><br />
<!-- Alias input -->
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
{{ form.select_alias(value=outloop.index) }}
</span>
<span class="input-group-addon" id="basic-addon2">Alias of</span>
{{ form.alias_of(class_="form-control") }}
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div> <!-- /row -->
<!-- END Alias input -->
<!-- New Entry input -->
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
{{ form.select_uinput(value=outloop.index) }}
</span>
<span class="input-group-addon" id="basic-addon2">New Entry</span>
{{ form.user_input(placeholder=form.title, class_="form-control") }}
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div> <!-- /row -->
</div>
<!-- END Alias input -->
{% endfor %}
{{ buttons.back() }} {{ buttons.next() }} {{ buttons.save() }}
</form>
{% endblock %}
答案 0 :(得分:2)
选项1:为每个表单实例its own prefix:
for pif in xrange(start, end+1):
pf = ProblemForm(request.form, title=session["problems"][pif][0], prefix='problem-%d' % pif)
# etc
选项2:或者,如果您使用FieldList FormFields提出封闭表单,WTForms可以为您生成带前缀的表单。
class EnclosingForm(Form):
problems = FieldList(FormField(ProblemForm))
然后使用append_entry在循环中添加子表单:
form = EnclosingForm(request.form)
for i, pif in enumerate(xrange(start, end+1)):
if len(form.problems.entries) <= i:
form.problems.append_entry({'title': session["problems"][pif][0]})
# etc