你并不需要使用
getlist,只是
get如果只有一个给定名称的输入,尽管它不应该的事。你显示的内容确实有效。这是一个简单的可运行示例:
from flask import Flask, requestapp = 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()提交带有两个复选框的表单,然后
['world', 'davidism']在终端中打印。请注意,html表单的方法是,post因此数据将位于中
request.form。
在某些情况下,了解字段的实际值或值列表很有用,看起来你只关心是否已选中该框即可。在这种情况下,更常见的是为复选框指定一个唯一的名称,然后仅检查它是否具有任何值。
<input type="checkbox" name="match-with-pairs"/><input type="checkbox" name="match-with-bears"/>
if request.form.get('match-with-pairs'): # match with pairsif request.form.get('match-with-bears'): # match with bears (terrifying)


