按照Flask Tutorial挖坑遭遇
RuntimeError: Working outside of application context.
求解半天芥喇,也沒(méi)看懂。因?yàn)閯側(cè)腴T(mén)凰萨,看資料也看不懂继控。Stackoverflow上倒是有一個(gè)類(lèi)似的答案,可是我沒(méi)看懂胖眷。武通。
下載了官方文檔的代碼,運(yùn)行珊搀,同樣 的報(bào)錯(cuò)冶忱。
沒(méi)辦法,硬著頭皮看下去境析,終于在若干個(gè)頁(yè)面之后找到了這個(gè)解決方案:
Manually Push a Context
If you try to access current_app
, or anything that uses it, outside an application context, you’ll get this error message:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in some way.
To solve this, set up an application context with app.app_context().
If you see that error while configuring your application, such as when initializing an extension, you can push a context manually since you have direct access to the app
. Use app_context()
in a with
block, and everything that runs in the block will have access to current_app
.
def create_app():
app = Flask(__name__)
with app.app_context():
init_db()
return app
If you see that error somewhere else in your code not related to configuring the application, it most likely indicates that you should move that code into a view function or CLI command.
順便發(fā)一張爬坑成功的截圖: