Python 烧瓶:属性值无
以下是我的代码块:Python 烧瓶:属性值无,python,flask,Python,Flask,以下是我的代码块: 下面一个是覆盖Flask Blueprint并添加属性config,该属性复制应用程序的配置 # Overriding Flask Blueprint from flask import Blueprint class CustomBlueprint(Blueprint): def __init__(self, name, import_name): super().__init__(name, import_name) self
下面一个是覆盖Flask Blueprint并添加属性config,该属性复制应用程序的配置
# Overriding Flask Blueprint
from flask import Blueprint
class CustomBlueprint(Blueprint):
def __init__(self, name, import_name):
super().__init__(name, import_name)
self.config = None
self.guard = None
def register(self, app, options, first_registration=False):
self.config = app.config
self.guard = app.guard
print(self.guard) # Works Fine
print(self.config) # Works Fine
super(CustomBlueprint, self).register(app, options, first_registration)
下面一个正在使用该类
from src.SHARED.overriden.blueprint import CustomBlueprint
from .guard import module_guard
example = CustomBlueprint('EXAMPLE', __name__)
print(example.config) # Here is the problem: It prints None
@example.route('/')
@module_guard
def hello_example():
return "Example is Working !!"
我还尝试了另一种方法,使用@property、getter和setter。但这会产生一个错误,属性错误:无法添加属性
我想访问/打印app.config,我应该怎么做???您可以使用
当前的app.config
访问蓝图上的app.config
来自flask导入蓝图,当前应用程序
from.guard导入模块\u guard
示例=蓝图(“示例”,“名称”)
#想要在此处访问当前的应用程序,例如打印(当前的应用程序配置)
@示例.路由(“/”)
@模块保护
def hello_示例():
返回“{}”格式(当前的_app.config.get('ENV'))
没有工作的兄弟!!错误:“断章取义”即使在我参考了当前应用程序的文档并相应地这样做之后,我的意图即将知道。。。为什么没有?只有在推送应用程序上下文时,例如在请求期间,您才能访问当前应用程序,您可以发布一个您试图使用配置执行的示例吗?是的,我知道,它在route/endpoint的函数中运行良好,但是我想在请求函数之外访问它,例如“from flask import Blueprint,current_app from.guard import module_guard example=Blueprint('example',name)#比如说,我想在这里打印app.config,例如,print(current_app.config)#将上下文抛出她的@example.route('/')@module_guard def hello_example():返回“{}”。格式(当前的_app.config.get('ENV'))35;在这里,它可以正常工作```