2014-03-13 53 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