Flask应用程序的配置文件

时间:2015-08-08 17:01:54

标签: python flask

我是Python的新手,所以请耐心等待。

我正在尝试创建一个文件,用于将配置设置存储在Flask项目中。但是,当我尝试导入文件时,我似乎遇到了错误。

这是我的配置文件(位置:app/config.py):

database_uri = 'something here'
secret_key = something here"

这就是我使用它的位置(位置:app/models.py):

from app import config
...
app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri

但是,我在启动应用程序时似乎遇到了这个错误:

[Sat Aug 08 19:00:15.539773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Target WSGI script '/var/www/pwforum/pwforum.wsgi' cannot be loaded as Python module.
[Sat Aug 08 19:00:15.540014 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Exception occurred processing WSGI script '/var/www/pwforum/pwforum.wsgi'.
[Sat Aug 08 19:00:15.540146 2015] [:error] [pid 29784] [client 188.183.57.54:64122] Traceback (most recent call last):
[Sat Aug 08 19:00:15.540250 2015] [:error] [pid 29784] [client 188.183.57.54:64122]   File "/var/www/pwforum/pwforum.wsgi", line 7, in <module>
[Sat Aug 08 19:00:15.540448 2015] [:error] [pid 29784] [client 188.183.57.54:64122]     from app import app as application
[Sat Aug 08 19:00:15.540537 2015] [:error] [pid 29784] [client 188.183.57.54:64122]   File "/var/www/pwforum/app/__init__.py", line 12, in <module>
[Sat Aug 08 19:00:15.540685 2015] [:error] [pid 29784] [client 188.183.57.54:64122]     from app import views, models
[Sat Aug 08 19:00:15.540773 2015] [:error] [pid 29784] [client 188.183.57.54:64122]   File "/var/www/pwforum/app/views.py", line 3, in <module>
[Sat Aug 08 19:00:15.541061 2015] [:error] [pid 29784] [client 188.183.57.54:64122]     from app.models import db, User, Category, Topic, Post
[Sat Aug 08 19:00:15.541154 2015] [:error] [pid 29784] [client 188.183.57.54:64122]   File "/var/www/pwforum/app/models.py", line 11, in <module>
[Sat Aug 08 19:00:15.541333 2015] [:error] [pid 29784] [client 188.183.57.54:64122]     app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
[Sat Aug 08 19:00:15.541413 2015] [:error] [pid 29784] [client 188.183.57.54:64122] AttributeError: 'module' object has no attribute 'database_uri'

1 个答案:

答案 0 :(得分:0)

您的配置文件应如下所示:

SQLALCHEMY_DATABASE_URI = '<your-db-driver>://<user>:<pw>@<db-url>'
SECRET_KEY = '<your-very-secret-key>'

然后你可以这样做:

app = Flask(__name__)
app.config.from_object('config')