遍历 WTForm class 字段

Loop over a WTForm class fields

如何循环遍历全部填充在 class

中的 WTForms

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

表格 classes

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.

抱歉,我没有足够的声誉来对此发表评论,所以我已经做了一个答案。

首先,为什么要创建一个字典来循环遍历request.form? request.form.keys() 可访问所有内容。并且之前不发送请求,对象 request 将不存在。因此,循环遍历表单对象,您可以将 request.form.keys()request.form[key]request.form.items()

结合使用

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

问候,托马斯