首页 > 解决方案 > 为什么在视图组件中设置隐藏字段值后在 Flask 中提交 WTForm 时,我的帖子结果中会出现重复变量?

问题描述

当我在视图组件中设置隐藏字段的值并提交表单时,另一端的 POST 变量返回每个变量的两个实例,其中第一个为空,但第二个包含预期数据。我似乎无法从 POST 变量(它是 ImmutableMultiDict 类型)中检索或弹出数据。如果我将 POST 转换为 dict 我会丢失包括数据在内的第二个变量。此外,如果我尝试遍历 POST 变量并构建字典,则不会复制第二个变量。因此,即使我接受这种双重性,基本上我也无法获得 POST 数据。

我在传统的 MVC 架构中使用 Flask 和 WTForms。

表格.py:

class TestForm(FlaskForm):
   the_int = HiddenField()
   the_string = HiddenField()
   the_list = HiddenField()
   the_dict = HiddenField()
   submit = SubmitField('Submit')

路线.py:

@app.route('/test')
def test():
    an_int = 1
    a_string = "ABC"
    a_list = ['One', 'Two']
    a_dict = {'First': 'A', 'Second': 'B'}
    form = TestForm()
    return render_template('test.html', an_int = an_int,
                                        a_string = a_string,
                                        a_list = a_list,
                                        a_dict = a_dict,
                                        form = form)


@app.route('/testresult', methods=['GET', 'POST'])
def testresult():
    if request.method == "POST":
        formdata = request.form
        dictdata = dict(request.form)
        builtdata = {}
        for k, v in request.form.items():
            builtdata.update({k: v})
        return render_template('testresult.html', formdata = formdata,
                                                  dictdata = dictdata,
                                                  builtdata = builtdata)
    return render_template('test')

测试.html:

<h1>Test</h1>
<br>
<form action="testresult" method="POST">
    {{ form.hidden_tag() }}
    {{ form.the_int(value=an_int) }}
    {{ form.the_string(value=a_string) }}
    {{ form.the_list(value=a_list) }}
    {{ form.the_dict(value=a_dict) }}
    {{ form.submit() }}
</form>

测试结果.html:

<h1>Result</h1>
<p>{{ formdata }}</p>
<p>{{ dictdata }}</p>
<p>{{ builtdata }}</p>

结果为 testresult.html :

Result

ImmutableMultiDict([('the_int', ''), ('the_int', '1'), ('the_string', ''), ('the_string', 'ABC'), ('the_list', ''), ('the_list', "['One', 'Two']"), ('the_dict', ''), ('the_dict', "{'First': 'A', 'Second': 'B'}"), ('csrf_token', 'IjN---kWw'), ('submit', 'Submit')])

{'the_int': '', 'the_string': '', 'the_list': '', 'the_dict': '', 'csrf_token': 'IjN---kWw', 'submit': 'Submit'}

{'the_int': '', 'the_string': '', 'the_list': '', 'the_dict': '', 'csrf_token': 'IjN---kWw', 'submit': 'Submit'}

标签: pythonflaskwtforms

解决方案


form.hidden_tag()呈现表单中的所有隐藏字段。您还手动渲染隐藏字段,因此它们最终会被定义两次。

这是有关 hidden_​​tag 的文档

编写模板的正确方法是:

<h1>Test</h1>
<br>
<form action="testresult" method="POST">
    {{ form.hidden_tag() }}
    {{ form.submit() }}
</form>

要将值插入隐藏字段,请在视图函数中执行:

    form = TestForm()
    form.the_int.data = an_int
    form.the_string.data = a_string
    form.the_list.data = a_list
    form.the_dict.data = a_dict
    return render_template('test.html', form = form)

推荐阅读