获取同名多个请求参数 [英] Get multiple request params of the same name
问题描述
我的问题是,用给定的代码:
from flask import Flask,请求
app = Flask(__ name__)
@ app.route(/)
def hello():
return str(request.values.get(param,无))
app.run(debug = True)
和我访问:
http:// localhost:5000 /?param = a& param = bbb
除Flask之外,我应该预期['a','bbb']的输出似乎只接受第一个参数,而忽略其余部分。
这是Flask的限制吗?你可以使用 getlist
,这是一种设计吗?
getList
,但由于某些原因,在Flask文档中没有提到: return str(request.args.getlist('param'))
结果是:
[u'a',u'bbb']
$ b 如果参数在查询字符串中(如问题中),则使用 request.args
c $ c> request.form 如果这些值来自多个具有相同名称的表单输入。 request.values
将两者结合在一起,但通常应避免使用更具体的集合。
My problem is that with the given code:
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def hello():
return str(request.values.get("param", "None"))
app.run(debug=True)
and I visit:
http://localhost:5000/?param=a¶m=bbb
I should expect an output of ['a', 'bbb'] except Flask seems to only accept the first param and ignore the rest.
Is this a limitation of Flask? Or is it by design?
You can use getlist
, which is similar to Django's getList
but for some reason isn't mentioned in the Flask documentation:
return str(request.args.getlist('param'))
The result is:
[u'a', u'bbb']
Use request.args
if the param is in the query string (as in the question), request.form
if the values come from multiple form inputs with the same name. request.values
combines both, but should normally be avoided for the more specific collection.
这篇关于获取同名多个请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!