Python 如何检查烧瓶中是否存在获取参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14234063/
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
How to check for the existence of a get parameter in flask
提问by Thomas Abraham
I'm new to python and flask.
我是 python 和烧瓶的新手。
I know that I can fetch a GET parameter with request.args.get(varname);. I wanted to check whether a GET request to my server is specifying and optional parameter or not.
我知道我可以使用 request.args.get(varname); 获取 GET 参数。我想检查对我的服务器的 GET 请求是否指定了可选参数。
Flask documentation didn't helped much.
Flask 文档没有多大帮助。
采纳答案by Jakob Bowyer
You can actually use the default value,
您实际上可以使用默认值,
opt_param = request.args.get("something")
if opt_param is None:
print "Argument not provided"
回答by James Akwuh
page = request.args.get("page", 0, type=int)
回答by kregus
A more Pythonic way to do the same would be using the inoperator:
一种更 Pythonic 的方法是使用in运算符:
if 'varname' in request.args:
# parameter 'varname' is specified
varname = request.args.get('varname')
else:
# parameter 'varname' is NOT specified
回答by Cilas Amos
You can check it with this code:
您可以使用以下代码进行检查:
name = request.args.get("name", default=None, type=str)

