首页 > 解决方案 > 循环遍历 WTForm 类字段

问题描述

我如何遍历一个类中全部填充的 WTForms

我测试的只是使用它周围的循环

表格类

class someForm(FlaskForm):
     some_filled_one = StringField('some_filled_one')
     some_filled_two = StringField('some_filled_two')
     ...

然后我有另一个地方是我想遍历这个字段。

dict = {"some_filled_one" : "some text", "some_filled_two" : "some text 2"}
form = someForm()
for key in dict.keys():
     response = request.form[key]
     ... #do some thing 

这给了我一个错误:

werkzeug.exceptions.BadRequestKeyError
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

标签: pythonpython-3.xflaskflask-wtforms

解决方案


抱歉,我没有足够的声誉对此发表评论,所以我确实得出了一个答案。

首先,为什么要创建一个字典来循环 request.form?一切都可以通过request.form.keys(). 并且之前没有发送请求,该对象request将不存在。因此,循环通过您可以request.form.keys()结合使用的表单对象request.form[key]request.form.items()

其次,我的猜测是之后发生的任何事情都#do some thing可能是错误的。

问候,托马斯


推荐阅读