未在app.extensions中注册烧瓶扩展 [英] Flask extension not registered in app.extensions

查看:72
本文介绍了未在app.extensions中注册烧瓶扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问在我的Flask应用中注册的某些扩展.我尝试使用 app.extensions ,但是我初始化的某些扩展不在字典中.

I want to access some extensions registered on my Flask app. I tried using app.extensions, but some of the extensions that I initialized aren't in the dict.

from flask import current_app
current_app.extensions.get(plugin_name)

extensions.py :

from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_mail import Mail
from flask_debugtoolbar import DebugToolbarExtension
from flask_images import Images
from flask_redis import FlaskRedis
from flask_plugins import PluginManager

db = SQLAlchemy()
migrate = Migrate()
mail = Mail()
toolbar = DebugToolbarExtension()
images = Images()
redis = FlaskRedis()
plugin_manager = PluginManager()

main.py :

def configure_extensions(app):
    db.init_app(app)
    migrate.init_app(app, db)
    mail.init_app(app)
    toolbar.init_app(app)
    images.init_app(app)
    redis.init_app(app)
    plugin_manager.init_app(
        app,
        plugin_folder="app/plugins",
        plugin_import_path='app.plugins',
    )

当我查看 current_app.extensions 时,我只看到了一些扩展名. plugin_manager toolbar 不在列表中.

When I look at current_app.extensions, I only see some of the extensions. plugin_manager and toolbar are not in the list.

>>> for key in app.extensions: print(key)
migrate
mail
images
sqlalchemy
redis

为什么某些扩展名未在 app.extensions 中注册?我是否对它们进行了错误的初始化?如何获得扩展?

Why aren't some extensions registered in app.extensions? Am I initializing them incorrectly? How can I get the extensions?

推荐答案

扩展名不需要将其添加到 app.extensions 中,它是可选的便利功能.显然,这些扩展名不会添加自己.如果需要,请考虑向相关项目提交补丁.

Extensions aren't required to add themselves to app.extensions, it's provided as an optional convenience. Apparently, those extensions don't add themselves. If you want that, consider submitting a patch to the relevant projects.

您无需使用 app.extensions 即可访问扩展实例.而是从myapp.extensions import db 导入创建的实例,例如.这就是能够分别创建它们和初始化它们的关键所在.在应用程序上下文中(例如在请求期间)使用时,对象将知道要使用的应用程序.

You don't need to use app.extensions to access the extension instances. Instead, import the instances you created, such as from myapp.extensions import db. That's the point of being able to create them separately from initializing them. When used in an app context (such as during a request), the objects will know what app to use.

这篇关于未在app.extensions中注册烧瓶扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆