在应用程序工厂中创建Flask扩展名时将其导入 [英] Import Flask extension when it is created in an app factory
问题描述
我想在应用程序的不同模块中使用Flask-Perm的装饰器.我正在使用应用程序工厂模式.如果在工厂中创建扩展名,则无法导入该扩展名以在其他模块中使用.使用应用程序工厂时如何导入扩展程序?
I want to use Flask-Perm's decorators in different modules in my application. I am using the app factory pattern. If I create the extension in the factory, I can't import it to use in the other modules. How can I import an extension when using an app factory?
from flask_perm import Perm
def create_app():
app = Flask(__name__)
perm = Perm(app)
return app
推荐答案
Flask-Perm的文档在顶部显示如何执行此操作.
Flask-Perm's docs show how to do this right at the top.
在工厂外创建扩展名.在工厂内部初始化扩展.每个支持应用程序工厂的Flask扩展都使用此模式.
Create the extension outside the factory. Init the extension inside the factory. Every Flask extension that supports app factories uses this pattern.
perm = Perm()
def create_app():
app = Flask(__name__)
perm.init_app(app)
return app
现在,您可以在需要访问的任何地方使用from myproject import perm
.
Now you can use from myproject import perm
wherever you need to access it.
这篇关于在应用程序工厂中创建Flask扩展名时将其导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!