Flask 教程填坑记

按照Flask Tutorial挖坑遭遇

RuntimeError: Working outside of application context.

求解半天,也没看懂。因为刚入门,看资料也看不懂。Stackoverflow上倒是有一个类似的答案,可是我没看懂。。

下载了官方文档的代码,运行,同样 的报错。

没办法,硬着头皮看下去,终于在若干个页面之后找到了这个解决方案:

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.

顺便发一张爬坑成功的截图:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容