wtforms 提交错误的表格
wtforms submitting wrong form
我正在 Flask 中制作一个网络应用程序,在一个页面上我有几个表单,当我点击一个表单(名为 'UsedBatch' 的表单)的提交按钮时,它最终提交了它好像它是另一种形式(称为 'NewBatch' 的形式),我不明白为什么。表单定义如下所示:
class NewBatch(FlaskForm):
quantity = IntegerField('Number of items:', validators=[DataRequired()])
date = DateField('Date:', default = date.today(), validators=[DataRequired()])
submit = SubmitField('Submit')
class UsedBatch(FlaskForm):
quantity = IntegerField('Number of items used:', validators=[DataRequired()])
date = DateField('Date:', default = date.today(), validators=[DataRequired()])
used_by = StringField('Used by:')
submit = SubmitField('Submit')
在通过模板 运行 之前,添加了一些表单属性,然后定义如下所示:
for items in items_list:
setattr(NewBatch, 'item_no', IntegerField(default = items.item_no))
setattr(UsedBatch, 'item_no', IntegerField(default = items.item_no))
processed_items_list.append({'item_no':items.item_no, 'quantity':items.quantity, 'items':items.items, 'batch_form':NewBatch(), 'used_form':UsedBatch()})
used_form = UsedBatch()
batch_form = NewBatch()
最后,模板中表单的代码如下所示,我遍历一个列表(上面显示的 'processesd_items_list'_ 其中列表中的每个元素都有一个附加表单,在这种情况下是可迭代的称为项目。第一种形式(UsedBatch)是:
<form method="POST">
{{ items['used_form'].date.label }}{{ items['used_form'].date(class="uk-input") }}
{{ items['used_form'].quantity.label }}{{ items['used_form'].quantity(class="uk-input") }}
{{ items['used_form'].used_by.label }}{{ items['used_form'].used_by(class="uk-input") }}
{{ items['used_form'].hidden_tag.label }}{{ items['used_form'].hidden_tag() }}
{{ items['used_form'].submit() }}
</form>
第二种形式(NewBatch)是:
<form method="POST">
{{ items['batch_form'].date.label }}{{ items['batch_form'].date(class="uk-input") }}
{{ items['batch_form'].quantity.label }}{{ items['batch_form'].quantity(class="uk-input") }}
{{ items['batch_form'].hidden_tag.label }}{{ items['batch_form'].hidden_tag() }}
{{ items['batch_form'].submit() }}
</form>
谁能看出为什么它会提交 NewBatch 而不是 UsedBatch?当我只添加行时:
if batch_form.validate_on_submit():
print('Batch submitted')
if used_form.validate_on_submit():
print('Usage submitted')
它 returns "Batch submitted" 而不是 "Usage submitted" 当提交 UsedBatch 表单时。谁能帮忙指出为什么?谢谢!
在您的 POST 路线上添加过滤器应该可以解决问题,请尝试以下操作:
if batch_form.submit.data and batch_form.validate(): # notice the order
print('Batch submitted')
if used_form.submit.data and used_form.validate(): # notice the order
print('Usage submitted')
当您调用 form.validate_on_submit() 时,无论单击哪个提交按钮,它都会检查表单是否通过 HTTP 方法提交。所以上面的小技巧就是添加一个过滤器(检查提交是否有数据,即batch_form.submit.data)。
参考:Multiple forms in a single page using flask and WTForms
我正在 Flask 中制作一个网络应用程序,在一个页面上我有几个表单,当我点击一个表单(名为 'UsedBatch' 的表单)的提交按钮时,它最终提交了它好像它是另一种形式(称为 'NewBatch' 的形式),我不明白为什么。表单定义如下所示:
class NewBatch(FlaskForm):
quantity = IntegerField('Number of items:', validators=[DataRequired()])
date = DateField('Date:', default = date.today(), validators=[DataRequired()])
submit = SubmitField('Submit')
class UsedBatch(FlaskForm):
quantity = IntegerField('Number of items used:', validators=[DataRequired()])
date = DateField('Date:', default = date.today(), validators=[DataRequired()])
used_by = StringField('Used by:')
submit = SubmitField('Submit')
在通过模板 运行 之前,添加了一些表单属性,然后定义如下所示:
for items in items_list:
setattr(NewBatch, 'item_no', IntegerField(default = items.item_no))
setattr(UsedBatch, 'item_no', IntegerField(default = items.item_no))
processed_items_list.append({'item_no':items.item_no, 'quantity':items.quantity, 'items':items.items, 'batch_form':NewBatch(), 'used_form':UsedBatch()})
used_form = UsedBatch()
batch_form = NewBatch()
最后,模板中表单的代码如下所示,我遍历一个列表(上面显示的 'processesd_items_list'_ 其中列表中的每个元素都有一个附加表单,在这种情况下是可迭代的称为项目。第一种形式(UsedBatch)是:
<form method="POST">
{{ items['used_form'].date.label }}{{ items['used_form'].date(class="uk-input") }}
{{ items['used_form'].quantity.label }}{{ items['used_form'].quantity(class="uk-input") }}
{{ items['used_form'].used_by.label }}{{ items['used_form'].used_by(class="uk-input") }}
{{ items['used_form'].hidden_tag.label }}{{ items['used_form'].hidden_tag() }}
{{ items['used_form'].submit() }}
</form>
第二种形式(NewBatch)是:
<form method="POST">
{{ items['batch_form'].date.label }}{{ items['batch_form'].date(class="uk-input") }}
{{ items['batch_form'].quantity.label }}{{ items['batch_form'].quantity(class="uk-input") }}
{{ items['batch_form'].hidden_tag.label }}{{ items['batch_form'].hidden_tag() }}
{{ items['batch_form'].submit() }}
</form>
谁能看出为什么它会提交 NewBatch 而不是 UsedBatch?当我只添加行时:
if batch_form.validate_on_submit():
print('Batch submitted')
if used_form.validate_on_submit():
print('Usage submitted')
它 returns "Batch submitted" 而不是 "Usage submitted" 当提交 UsedBatch 表单时。谁能帮忙指出为什么?谢谢!
在您的 POST 路线上添加过滤器应该可以解决问题,请尝试以下操作:
if batch_form.submit.data and batch_form.validate(): # notice the order
print('Batch submitted')
if used_form.submit.data and used_form.validate(): # notice the order
print('Usage submitted')
当您调用 form.validate_on_submit() 时,无论单击哪个提交按钮,它都会检查表单是否通过 HTTP 方法提交。所以上面的小技巧就是添加一个过滤器(检查提交是否有数据,即batch_form.submit.data)。
参考:Multiple forms in a single page using flask and WTForms