Python 获取多个同名请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14188451/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:42:41  来源:igfitidea点击:

Get multiple request params of the same name

pythonflask

提问by John Jiang

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&param=bbb

I should expect an output of ['a', 'bbb'] except Flask seems to only accept the first param and ignore the rest.

我应该期待 ['a', 'bbb'] 的输出,除了 Flask 似乎只接受第一个参数而忽略其余参数。

Is this a limitation of Flask? Or is it by design?

这是 Flask 的限制吗?还是设计使然?

采纳答案by Blender

You can use getlist, which is similar to Django's getListbut for some reason isn't mentioned in the Flask documentation:

您可以使用getlist,它类似于 Django 的,getList但由于某种原因在 Flask 文档中没有提到:

return str(request.args.getlist('param'))

The result is:

结果是:

[u'a', u'bbb']

Use request.argsif the param is in the query string (as in the question), request.formif the values come from multiple form inputs with the same name. request.valuescombines both, but should normally be avoided for the more specific collection.

使用request.args如果参数是查询字符串(如题),request.form如果值来自具有相同名称的多个表单输入。 request.values结合两者,但通常应避免用于更具体的集合。

回答by Vin

If you used $('form').serialize()in jQuery to encode your form data, you can use request.form['name']to get data, but note that when multiple input elements' names are the same, request.form['name']will only get the first matched item. So I checked the form object from the Flask API, and I found this. Then I checked the MultiDict object, and I found the function getlist('name').

如果$('form').serialize()在 jQuery 中对表单数据进行编码,则可以使用request.form['name']获取数据,但请注意,当多个输入元素的名称相同时,request.form['name']只会获取第一个匹配项。所以我检查了 Flask API 中的表单对象,我发现了这个. 然后我检查了MultiDict 对象,我找到了函数getlist('name')

If there are multiple inputs with the same name, try this method: request.form.getlist('name')

如果有多个同名输入,试试这个方法: request.form.getlist('name')

回答by Vin

One more way is that you can use one key and one value that holds multiple values and then in the server you can split and do what ever you want .Hope this Helps somewone

另一种方法是您可以使用一个键和一个值来保存多个值,然后在服务器中您可以拆分并做任何您想做的事情。希望这对某人有所帮助

http://localhost/api/products/filters?Manufacturer=Dell|HP|HUAWEI|Lenovo

OR

或者

http://localhost/api/products/filters?Manufacturer=Dell_HP_HUAWEI_Lenovo

OR

或者

http://localhost/api/products/filters?Manufacturer=Dell__HP__HUAWEI__Lenovo

回答by Nebulastic

Another option is to use a flat json structure with request.args. Because sometimes you simply do not know the parameter beforehand and you cannot use .getlist().

另一种选择是使用带有 request.args 的平面 json 结构。因为有时您根本不知道参数而无法使用.getlist().

arguments = request.args.to_dict(flat=False)

# Get a specific parameter
params = arguments.get('param')
print(params)

# Get all the parameters that have more than one value
for field, values in arguments.items():
    if len(values) > 1:
        print(values)