Python 获取 Flask 中复选框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31859903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of a checkbox in Flask
提问by Jaquacky
I want to get the value of a checkbox in Flask. I've read a similar postand tried to use the output of request.form.getlist('match')
and since it's a list I use [0]
, but it seems I'm doing something wrong. Is this the correct way to get the output or is there a better way?
我想获取 Flask 中复选框的值。我读过类似的帖子并尝试使用request.form.getlist('match')
和 因为它是我使用的列表[0]
,但似乎我做错了什么。这是获取输出的正确方法还是有更好的方法?
<input type="checkbox" name="match" value="matchwithpairs" checked> Auto Match
if request.form.getlist('match')[0] == 'matchwithpairs':
# do something
采纳答案by davidism
You don't need to use getlist
, just get
if there's only one input with the given name, although it shouldn't matter. What you've shown does work. Here's a simple runnable example:
您不需要使用getlist
,只要get
只有一个具有给定名称的输入,尽管这无关紧要。你所展示的确实有效。这是一个简单的可运行示例:
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
print(request.form.getlist('hello'))
return '''<form method="post">
<input type="checkbox" name="hello" value="world" checked>
<input type="checkbox" name="hello" value="davidism" checked>
<input type="submit">
</form>'''
app.run()
Submitting the form with both boxes checked prints ['world', 'davidism']
in the terminal. Note that the html form's method is post
so that the data will be in request.form
.
提交带有两个复选框的表单会['world', 'davidism']
在终端中打印。请注意,html 表单的方法是post
使数据在request.form
.
While there are some cases where knowing the actual value or list of values of an field is useful, it looks like all you care about is whether the box was checked. In this case, it's more common to give the checkbox a unique name and just check if it has any value at all.
虽然在某些情况下了解字段的实际值或值列表很有用,但看起来您只关心是否选中了该框。在这种情况下,更常见的是给复选框一个唯一的名称,然后检查它是否有任何值。
<input type="checkbox" name="match-with-pairs"/>
<input type="checkbox" name="match-with-bears"/>
if request.form.get('match-with-pairs'):
# match with pairs
if request.form.get('match-with-bears'):
# match with bears (terrifying)
回答by Silas Santiago
I found 4 ways to do that: Just to summarize:
我找到了 4 种方法来做到这一点: 总结一下:
# first way
op1 = request.form.getlist('opcao1') # [u'Item 1'] []
op2 = request.form.getlist('opcao2') # [u'Item 2'] []
op3 = request.form.getlist('opcao3') # [u'Item 3'] []
# second
op1_checked = request.form.get("opcao1") != None
op2_checked = request.form.get("opcao2") != None
op3_checked = request.form.get("opcao3") != None
# third
if request.form.get("opcao3"):
op1_checked = True
# fourth
op1_checked, op1_checked, op1_checked = False, False, False
if request.form.get("opcao1"):
op1_checked = True
if request.form.get("opcao2"):
op2_checked = True
if request.form.get("opcao3"):
op3_checked = True
# last way that I found ..
op1_checked = "opcao1" in request.form
op2_checked = "opcao2" in request.form
op3_checked = "opcao3" in request.form