Python 如何获取flask中get请求的参数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22383458/
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 obtain values of parameters of get request in flask?
提问by Roman
The answer that I found on the web is to use request.args.get. However, I cannot manage it to work. I have the following simple example:
我在网上找到的答案是使用request.args.get. 但是,我无法让它工作。我有以下简单示例:
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def hello():
print request.args['x']
return "Hello World!"
if __name__ == "__main__":
app.run()
I go to the 127.0.0.1:5000/hello?x=2in my browser and as a result I get:
我转到127.0.0.1:5000/hello?x=2浏览器中的 ,结果我得到:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
What am I doing wrong?
我究竟做错了什么?
采纳答案by sberry
The simple answer is you have not imported the requestglobal object from the flask package.
简单的答案是您没有request从烧瓶包中导入全局对象。
from flask import Flask, request
This is easy to determine yourself by running the development server in debug mode by doing
这很容易通过在调试模式下运行开发服务器来确定自己
app.run(debug=True)
This will give you a stacktrace including:
这将为您提供堆栈跟踪,包括:
print request.args['x']
NameError: global name 'request' is not defined
回答by Joe9008
http://localhost:5000/api/iterators/opel/next?n=5
http://localhost:5000/api/iterators/opel/next ?n=5
For something like the case before
对于类似之前的情况
from flask import Flask, request
n = request.args.get("n")
Can do the trick
可以做的伎俩

