从其他文件访问Flask中的配置值 [英] Access config values in Flask from other files

查看:51
本文介绍了从其他文件访问Flask中的配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下应用程序结构:

I have the following app structure:

manage.py
myapp/
  __init__.py
  config.py
  views/
    __init__.py
    login.py
    ...

myapp/__ init __.py 中,我有一个函数 create_app(),该函数返回Flask应用程序实例.配置值也在 create_app()中进行了说明.我希望能够在其他文件(例如 login.py )中访问这些值.我尝试过:

In myapp/__init__.py I have a function create_app() which returns the Flask app instance. The config values are also stated in create_app() too. I would like to be able to access these values in other files such as login.py. I've tried:

from myapp import create_app as app
print app.config['SECRET_KEY']

但是我收到一条错误消息,指出 AttributeError:'function'对象没有属性'config'

However I receive an error stating AttributeError: 'function' object has no attribute 'config'

我在做什么错,我该如何解决?谢谢.

What am I doing wrong and how can I fix this? Thanks.

推荐答案

使用烧瓶导入current_app 中的.您可以在 settings.py 中定义 SECRET_KEY .

Use from flask import current_app. You define SECRET_KEY in settings.py.

打印current_app.config ['SECRET_KEY']

这篇关于从其他文件访问Flask中的配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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