python - 循环遍历 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.
解决方案
抱歉,我没有足够的声誉对此发表评论,所以我确实得出了一个答案。
首先,为什么要创建一个字典来循环 request.form?一切都可以通过request.form.keys()
. 并且之前没有发送请求,该对象request
将不存在。因此,循环通过您可以request.form.keys()
结合使用的表单对象request.form[key]
或request.form.items()
其次,我的猜测是之后发生的任何事情都#do some thing
可能是错误的。
问候,托马斯
推荐阅读
- apache-spark - 带有 Kotlin 和 Apache Spark 2.3 的胖 JAR
- objective-c - 回调强引用循环
- java - 将外部声明的 bean 标记为 @Primary
- git - hoek 节点模块的 Github 潜在安全漏洞错误
- ios - 具有不同图像尺寸并保持图像纵横比的布局
- python - AttributeError:“_mysql.connection”对象没有属性“光标”
- php - 使用 PHP 从 json 文件中删除对象
- git - “远程:未经授权”,“致命:身份验证失败...”(Bitbucket)
- c# - 在这种情况下如何正确使用泛型和类型约束?
- python - 在 for 循环中使用 str.split 时出现“ValueError 太多值无法解包”