小编典典

如何在蓝图中访问app.config?

flask

我正在尝试authorisation.py在包api中的蓝图内访问访问应用程序配置。我在初始化的蓝图__init__.py,其在使用authorisation.py

init.py

from flask import Blueprint
api_blueprint = Blueprint("xxx.api", __name__, None)
from api import authorisatio

authorisation.py

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)

阅读 791

收藏
2020-04-06

共1个答案

小编典典

你可以用来flask.current_app代替蓝图中的应用程序。

from flask import current_app as app
@api.route('/info/', methods = ['GET'])
def get_account_num():
    num = app.config["INFO"]

注:该current_app代理仅在的情况下可要求。

2020-04-06