Flask 如何在蓝图中访问app.config?
我正在尝试访问blueprintFlask 如何在蓝图中访问app.config?,flask,Flask,我正在尝试访问blueprintauthorization.py中的access应用程序配置,它位于程序包api中。我正在\uuu init\uuu.py中初始化蓝图,该蓝图在authorization.py中使用 __初始值 from flask import Blueprint api_blueprint = Blueprint("xxx.api", __name__, None) from api import authorisation 授权书 from flask import re
authorization.py中的access应用程序配置,它位于程序包api中。我正在\uuu init\uuu.py
中初始化蓝图,该蓝图在authorization.py
中使用
__初始值
from flask import Blueprint
api_blueprint = Blueprint("xxx.api", __name__, None)
from api import authorisation
授权书
from flask import request, jsonify, current_app
from ..oauth_adapter import OauthAdapter
from api import api_blueprint as api
client_id = current_app.config.get('CLIENT_ID')
client_secret = current_app.config.get('CLIENT_SECRET')
scope = current_app.config.get('SCOPE')
callback = current_app.config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
@api.route('/authorisation_url')
def authorisation_url():
url = auth.get_authorisation_url()
return str(url)
我遇到RuntimeError:在应用程序上下文之外工作
我明白这是为什么,但是访问这些配置设置的正确方法是什么
----更新----
我暂时这样做了
@api.route('/authorisation_url')
def authorisation_url():
client_id, client_secret, scope, callback = config_helper.get_config()
auth = OauthAdapter(client_id, client_secret, scope, callback)
url = auth.get_authorisation_url()
return str(url)
您需要导入由Flask()
返回的主app
变量(或您调用的任何变量):
或者在请求中执行此操作:
@api.route('/authorisation_url')
def authorisation_url():
client_id = current_app.config.get('CLIENT_ID')
url = auth.get_authorisation_url()
return str(url)
当你需要的时候,蓝图已经显示出来了。因此,您可以重写此方法或使用来描述依赖于app
的逻辑当前的\u app
方法很好,但您必须有一些请求上下文。如果你没有(比如测试等一些前期工作),你最好
与app.test\u request\u上下文('/'):
在此当前应用程序调用之前
您将遇到运行时错误:在应用程序上下文之外工作。要在答案的基础上构建,下面是一个覆盖该示例的示例:
以及使用以下公式的示例:
重载record
方法似乎很容易:
api_blueprint = Blueprint('xxx.api', __name__, None)
api_blueprint.config = {}
@api_blueprint.record
def record_params(setup_state):
app = setup_state.app
api_blueprint.config = dict([(key,value) for (key,value) in app.config.iteritems()])
您还可以将蓝图封装在函数中,并将应用程序
作为参数传递:
蓝图:
def get_blueprint(app):
bp = Blueprint()
return bp
主要内容:
在蓝图视图中,使用当前应用程序取代应用程序
from flask import current_app
@api.route("/info")
def get_account_num():
num = current_app.config["INFO"]
current\u应用程序
代理仅在请求上下文中可用。是的,我不想执行这两种操作中的任何一种。第一种方法是创建交叉引用,第二种方法不是干巴巴的。@Chirdeptomar如果第一种方法是创建循环导入(破坏应用程序),那么你的应用程序的结构就有问题。@DanielChatfield这根本不是真的。app对象是注册蓝图的对象。建议blueprint正确,然后导入应用程序对象将始终导致循环依赖。有关正确策略,请参阅其他答案。@sholsapp我知道它将创建循环导入(就像在flask文档中一样:),我说如果它创建的循环导入破坏了应用程序。@api.record“对我不起作用。“api”来自什么名称空间?抱歉,没有从api import api_blueprint的问题中的行中复制该名称空间作为api
当应用程序在工厂中创建,因此无法导入“app”(或任何称为flask应用程序的名称)时,会发生什么情况?在请求内部没有问题,因为在请求期间存在应用程序上下文,但在定义请求逻辑之外需要应用程序配置的部分时。如果无法使用应用程序创建上下文,如何访问应用程序配置?请注意,当前应用程序
代理仅在请求上下文中可用。@sephr关于如何从其他位置访问该请求上下文(不作为参数传递,而是作为某种全局参数传递)的任何提示?我尝试过,但得到了一个答案“内部服务器错误“.这种方法有什么缺点吗?@Tuukka:我不记得有什么特别的缺点,我已经很久没用了。”。在多个应用程序中使用blueprint时,使用flask.current_应用程序可能会有一些好处。我建议如果这种方法解决了您使用它的问题,Flask不会强制执行特定的方法。对于Python 3,请使用:app.config.items()而不是app.config.iteritems()嗨,我是否需要调用或注册record_参数,我已经尝试过,但没有成功。非常感谢。如果您需要访问应用程序(例如获取设置蓝图的配置),这非常棒<代码>api_blueprint
没有属性config
。如果我想这样做,我想我必须setattr
?
api_blueprint = Blueprint('xxx.api', __name__, None)
api_blueprint.config = {}
@api_blueprint.record
def record_params(setup_state):
app = setup_state.app
api_blueprint.config = dict([(key,value) for (key,value) in app.config.iteritems()])
def get_blueprint(app):
bp = Blueprint()
return bp
from . import my_blueprint
app.register_blueprint(my_blueprint.get_blueprint(app))
from flask import current_app
@api.route("/info")
def get_account_num():
num = current_app.config["INFO"]