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

编程入门 行业动态 更新时间:2024-10-25 14:32:30
本文介绍了如何在蓝图中访问app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在封装api中的蓝图authorisation.py中访问访问应用程序配置.我正在authorisation.py中使用的__init__.py中初始化蓝图.

I am trying to access access application configuration inside a blueprint authorisation.py which in a package api. I am initializing the blueprint in __init__.py which is used in authorisation.py.

__ init __.py

__init__.py

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

authorisation.py

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:在应用程序上下文之外工作

I am getting RuntimeError: working outside of application context

我知道为什么会这样,但是访问这些配置设置的正确方法是什么?

I understand why that is but then what is the correct way of accessing those configuration settings?

----更新---- 暂时,我已经做到了.

----Update---- Temporarily, I have done this.

@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.current_app代替app.

Use flask.current_app in place of app in the blueprint view.

from flask import current_app @api.route("/info") def get_account_num(): num = current_app.config["INFO"]

current_app代理仅在请求的上下文中可用.

The current_app proxy is only available in the context of a request.

更多推荐

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

本文发布于:2023-11-08 13:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   如何在   config   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!