2014-03-13 52 views
32

我在网上找到的答案是使用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() 

我去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. 

我在做什么错?

+1

它的一半左右一路下滑[快速启动页下的访问请求数据(http://flask.pocoo.org/docs/0.10/quickstart/#访问请求数据)在[context locals]下(http://flask.pocoo.org/docs/0.10/quickstart/#context-locals) –

回答

61

简单的答案是你没有从烧瓶包中导入request全局对象。

from flask import Flask, request 

这是很容易通过运行在调试模式下开发服务器做

app.run(debug=True) 

这会给你一个堆栈跟踪,包括确定自己:?

print request.args['x'] 
NameError: global name 'request' is not defined