在单个页面中使用烧瓶和WTForms的多种形式 [英] Multiple forms in a single page using flask and WTForms

查看:141
本文介绍了在单个页面中使用烧瓶和WTForms的多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个页面上有多个表单,发送post请求到相同的处理程序
in flask。



我使用wtforms生成表单。 >

识别提交表单的最佳方法是什么?



我正在使用 action = ?形式= oneform。我认为应该有一些更好的方法
来达到同样的效果吗?

解决方案

我一直在使用两个瓶子片段的组合。 首先增加了一个前缀到表单,然后用validate_on_submit()检查前缀。 我还使用LouisRoché的模板来确定在表单中按下哪些按钮



引用Dan Jacob:




$

  form1 = FormA(prefix =form1)
form2 = FormB(prefix =form2)
form3 = FormC前缀=form3)

然后添加一个隐藏字段(或者只是检查一个提交字段):

  if form1.validate_on_submit()and form1.submit.data:

引用LouisRoché的

/ p>

 < input type =submitname =btnvalue =Save> 
< input type =submitname =btnvalue =Cancel>

为了弄清楚哪个按钮已经通过服务器端,在我的views.py文件中: p>

  if request.form ['btn'] =='Save':
something0
else:
something1


I have multiple form on the same page that send post request to same handler in flask.

I am generating forms using wtforms.

what is the best way to identify which form is submitted ?

I am currently using action="?form=oneform". I think there should be some better method to achieve the same?

解决方案

I've been using a combination of two flask snippets. The first adds a prefix to a form and then you check for the prefix with validate_on_submit(). I use also Louis Roché's template to determine what buttons are pushed in a form.

To quote Dan Jacob:

Example:

form1 = FormA(prefix="form1")
form2 = FormB(prefix="form2")
form3 = FormC(prefix="form3")

Then, add a hidden field (or just check a submit field):

if form1.validate_on_submit() and form1.submit.data:

To quote Louis Roché's:

I have in my template :

<input type="submit" name="btn" value="Save">
<input type="submit" name="btn" value="Cancel">

And to figure out which button was passed server side I have in my views.py file:

if request.form['btn'] == 'Save':
    something0
else:
    something1

这篇关于在单个页面中使用烧瓶和WTForms的多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆