使用 Flask Factory 应用程序配置电子邮件令牌 create_app
Configure Email Token with Flask Factory Application create_app
一旦我开始使用 Flask 工厂应用程序模式,我就对配置和导入感到困惑。
我在#app/init.py 中创建一个函数为 create_app 的应用程序
我有一个用于设置 development/testing/production 变量的配置文件,以及一个包含另一个配置文件的实例文件夹。
def create_app(config_name):
app=Flask(__name__, instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
etc...
return app
我正在使用蓝图并在#app/auth/views.py 中有一个身份验证视图
我正在尝试使用 URLSafeTimedSerializer 设置电子邮件确认令牌...
from itsdangerous import URLSafeTimedSerializer
@auth.route('/register', methods=['GET','POST'])
def register():
ts = URLSafeTimedSerializer(app.config['SECRET_KEY'])
token = ts.dumps(self.email, salt='email-confirm-key')
etc...
现在我的问题是,我的变量 'ts' 需要 app.config['SECRET_KEY'] 集。但是我无法定义 app 变量(如所有在线教程中所示)。当我尝试导入时出现错误...(in #app/auth/views.py)
from .. import app
当我尝试导入时...
from .. import create_app
有人可以阐明如何在 flask 应用程序工厂 create_app 之外使用 'app' 和 app.config 初始化模块吗?
希望你理解我的问题。
在这种情况下,您应该使用 Flask.current_app
from flask import current_app
...
ts = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
flask.current_app
Points to the application handling the request. This
is useful for extensions that want to support multiple applications
running side by side. This is powered by the application context and
not by the request context, so you can change the value of this proxy
by using the app_context() method.
This link aso 解释了有关 Flask 应用程序工厂方法的更多详细信息,特别是使用 current_app
访问应用程序配置。
一旦我开始使用 Flask 工厂应用程序模式,我就对配置和导入感到困惑。
我在#app/init.py 中创建一个函数为 create_app 的应用程序 我有一个用于设置 development/testing/production 变量的配置文件,以及一个包含另一个配置文件的实例文件夹。
def create_app(config_name):
app=Flask(__name__, instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
etc...
return app
我正在使用蓝图并在#app/auth/views.py 中有一个身份验证视图 我正在尝试使用 URLSafeTimedSerializer 设置电子邮件确认令牌...
from itsdangerous import URLSafeTimedSerializer
@auth.route('/register', methods=['GET','POST'])
def register():
ts = URLSafeTimedSerializer(app.config['SECRET_KEY'])
token = ts.dumps(self.email, salt='email-confirm-key')
etc...
现在我的问题是,我的变量 'ts' 需要 app.config['SECRET_KEY'] 集。但是我无法定义 app 变量(如所有在线教程中所示)。当我尝试导入时出现错误...(in #app/auth/views.py)
from .. import app
当我尝试导入时...
from .. import create_app
有人可以阐明如何在 flask 应用程序工厂 create_app 之外使用 'app' 和 app.config 初始化模块吗?
希望你理解我的问题。
在这种情况下,您应该使用 Flask.current_app
from flask import current_app
...
ts = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
flask.current_app
Points to the application handling the request. This is useful for extensions that want to support multiple applications running side by side. This is powered by the application context and not by the request context, so you can change the value of this proxy by using the app_context() method.
This link aso 解释了有关 Flask 应用程序工厂方法的更多详细信息,特别是使用 current_app
访问应用程序配置。