获取多个同名请求参数 [英] Get multiple request params of the same name

查看:32
本文介绍了获取多个同名请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是给定的代码:

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)

我访问:

http://localhost:5000/?param=a&param=bbb

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

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

这是 Flask 的限制吗?还是有意为之?

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

推荐答案

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

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'))

结果是:

[u'a', u'bbb']

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆