使用应用工厂和烧瓶脚本时导入烧瓶应用程序
Importing Flask app when using app factory and flask script
这是 Flask 应用上下文
app = Flask(__name__)
with app.app_context():
# insert code here
应用上下文的大部分用例涉及 'app' 在同一脚本上初始化或从基础导入应用。
我的申请结构如下:
# application/__init__.py
def create_app(config):
app = Flask(__name__)
return app
# manage.py
from application import create_app
from flask_script import Manager
manager = Manager(create_app)
manager.add_command("debug", Server(host='0.0.0.0', port=7777))
这可能是个微不足道的问题,但如果我的应用程序结构如下,我应该如何调用 'with app.app_context()'?
Flask-Script 调用测试上下文中的所有内容,因此您可以使用 current_app 和其他习语:
The Manager runs the command inside a Flask test context. This means that you can access request-local proxies where appropriate, such as current_app, which may be used by extensions.
http://flask-script.readthedocs.org/en/latest/#accessing-local-proxies
因此您不需要将 with app.app_context() 与管理器脚本一起使用。如果你想做其他事情,那么你必须先创建应用程序:
from application import create_app
app = create_app()
with app.app_context():
# stuff here
这是 Flask 应用上下文
app = Flask(__name__)
with app.app_context():
# insert code here
应用上下文的大部分用例涉及 'app' 在同一脚本上初始化或从基础导入应用。
我的申请结构如下:
# application/__init__.py
def create_app(config):
app = Flask(__name__)
return app
# manage.py
from application import create_app
from flask_script import Manager
manager = Manager(create_app)
manager.add_command("debug", Server(host='0.0.0.0', port=7777))
这可能是个微不足道的问题,但如果我的应用程序结构如下,我应该如何调用 'with app.app_context()'?
Flask-Script 调用测试上下文中的所有内容,因此您可以使用 current_app 和其他习语:
The Manager runs the command inside a Flask test context. This means that you can access request-local proxies where appropriate, such as current_app, which may be used by extensions.
http://flask-script.readthedocs.org/en/latest/#accessing-local-proxies
因此您不需要将 with app.app_context() 与管理器脚本一起使用。如果你想做其他事情,那么你必须先创建应用程序:
from application import create_app
app = create_app()
with app.app_context():
# stuff here