我在路由中使用它从表单获取数据时遇到问题
forms.py
class Calculator(Form):
amount = IntegerField('Amount')
weight = IntegerField('Weight')
class Program(Form):
cycles = IntegerField('Cycles')
volume = FormField(Calculator)
app.py
@app.route('/', methods=('GET', 'POST'))
def index():
form = forms.Program()
if form.validate_on_submit():
values = models.Progression(
cycles=form.cycles.data,
amount=form.amount.data,
weight=form.weight.data
)
return render_template('index.html', form=form, values=values)
cycles 的数据通过得很好,但我不确定如何在我的路由中访问封装形式的语法。文档说 FormField 将返回所附表单的数据字典,但我似乎无法弄清楚如何获取它并将其放入变量中。
请您参考如下方法:
我能够使用
获取我需要的数据 amount=form.volume.amount.data,
weight=form.volume.weight.data
真正的问题是当我使用 FormField 时表单没有验证。我应该早点检查的菜鸟错误。 我必须通过从 flask_wtf 导入它并使用 CsrfProtect(app)
