pal*_*dot 8 design-patterns flask
我正在学习烧瓶和蟒蛇,并且不能围绕一个典型的烧瓶应用程序需要构建的方式.
我需要从内部蓝图访问应用程序配置.像这样的东西
#blueprint.py
from flask import Blueprint
sample_blueprint = Blueprint("sample", __name__)
# defining a route for this blueprint
@sample_blueprint.route("/")
def index():
# !this is the problematic line
# need to access some config from the app
x = app.config["SOMETHING"]
# how to access app inside blueprint?
Run Code Online (Sandbox Code Playgroud)
如果在蓝图中导入app是解决方案,这不会导致循环导入吗?即在应用程序中导入蓝图,在蓝图中导入应用程序?
rtz*_*zll 10
从关于appcontext的文档:
应用程序上下文为current_app上下文本地提供支持
应用于您的示例:
from flask import Blueprint, current_app
sample = Blueprint('sample', __name__)
@sample.route('/')
def index():
x = current_app.config['SOMETHING']
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的,这里是我整理的一个小要点,供参考。