我想生成这个JSON对象,其中包含来自表单输入的对象数组:
{
"network":[
{"layer_type": "conv2d", "num_filters": 16, "kernel_size": 2, "padding": "valid", "stride": 2},
{"layer_type": "max_pool2d", "num_filters": 16, "kernel_size": 2, "padding": "valid", "stride": 2},
{"layer_type": "conv2d", "num_filters": 32, "kernel_size": 3, "padding": "valid", "stride": 2}
]
}
有没有办法可以使用Flask做到这一点?
更新
这是表格的样子:
至于动态生成的html代码片段:
<li>
<select name="network[][layer_type]"><!-- options here --></select>
<input type="number" name="network[][num_filters]">
<!-- other parameters here -->
</li>
<li>
<select name="network[][layer_type]"><!-- options here --></select>
<input type="number" name="network[][num_filters]">
<!-- other parameters here -->
</li>
编辑:由于此问题被标记为重复,我将添加更多信息。我希望在question中实现类似的功能,但使用Flask:
{"students" => [
{
"first" => "foo",
"last" => "bar",
"age" => "21"
},
{
"first" => "baz",
"last" => "qux",
"age" => "19"
}
]}
根据已接受的答案,它可以与Ruby一起使用,具有以下形式:
<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
但我想知道如何使用Flask。
答案 0 :(得分:1)
form.data
例如
考虑下面定义的表格,
class GeneralForm(FlaskForm):
boolean_val = BooleanField('Boolean')
a_float = FloatField('Severity')
submit = SubmitField('Submit')
在应用路线中,
@app.route('/wtforms', methods=['GET', 'POST'])
def debug_wtforms():
form = GeneralForm()
if request.method == 'POST' and form.validate_on_submit():
print(form.data) # Form data as a dict
return render_template('index1.html', form=form)
request.form.to_dict(flat=False)
我希望这会有所帮助。