如何在 Flask 中获取提交表单的名称? [英] How to get the name of a submitted form in Flask?
问题描述
我正在使用 Flask 构建一个网站,在一个页面上我有两个表单.如果有 POST,我需要决定发布哪个表单.我当然可以从 request.form
中存在的字段中扣除它,但我宁愿通过获取名称(由 <form name="my_form">
) 提交的表单.我尝试了几种方法,例如:
I'm building a website using Flask, and on one page I've got two forms. If there's a POST, I need to decide which form is being posted. I can of course deduct it from the fields that are present in request.form
, but I would rather make it explicit by getting the name (defined by <form name="my_form">
) of the form that is submitted. I tried several things, such as:
@app.route('/myforms', methods=['GET', 'POST'])
def myForms():
if request.method == 'POST':
print request.form.name
print request.form.['name']
但不幸的是,没有任何效果.有谁知道我在哪里可以得到提交的表格的名称?欢迎所有提示!
but unfortunately, nothing works. Does anybody know where I can get the name of the form submitted? All tips are welcome!
推荐答案
没有表单名称".该信息不是由浏览器发送的;<form>
标签上的 name
属性旨在仅在浏览器端使用(不推荐启动,使用 id
代替).
There is no 'name of the form'. That information is not sent by the browser; the name
attribute on <form>
tags is meant to be used solely on the browser side (and deprecated to boot, use id
instead).
您可以使用隐藏字段添加该信息,但区分发布到同一表单处理程序的表单的最常见方法是为提交按钮命名:
You could add that information by using a hidden field, but the most common way to distinguish between forms posting to the same form handler is to give the submit button a name:
<submit name="form1" value="Submit!"/>
和
if 'form1' in request.form:
但您也可以使用 <input type="hidden">
字段来包含区分表单的方法.
but you could also use a <input type="hidden">
field to include the means to distinguish between forms.
这篇关于如何在 Flask 中获取提交表单的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!