在.env文件

时间:2017-02-14 21:01:23

标签: django cookiecutter-django

我用cookiecutter django创建了一个新项目。我设置了环境变量DJANGO_READ_DOT_ENV_FILE = True(将其设置为False也会导致.env文件被读取。我认为python假设环境变量True是一个字符串而不是bool )。 / p>

在.env文件中有以下行:

DJANGO_SETTINGS_MODULE=config.settings.production

当我跑步时

python manage.py runserver

我得到以下输出:

Loading : /home/bucket/src/b2b/.env
The .env file has been loaded. See common.py for more information
Loading : /home/bucket/src/b2b/.env
The .env file has been loaded. See common.py for more information
Performing system checks...

System check identified no issues (0 silenced).
February 14, 2017 - 20:20:55
Django version 1.10.5, using settings 'config.settings.local'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

根据以上所述,服务器配置为config.settings.local。我知道正在加载.env文件,因为DATABASE_URL变量是正确的并且它连接到数据库。

看起来从.env文件加载变量的代码正在运行两次。我在common.py和local.py中放了一些print语句来跟踪执行情况,结果如下。

Loading : /home/bucket/src/b2b/.env
The .env file has been loaded. See common.py for more information
End of common.py <--
End of local.py <--
Loading : /home/bucket/src/b2b/.env
The .env file has been loaded. See common.py for more information
End of common.py <--
End of local.py <--
Performing system checks...

System check identified no issues (0 silenced).
February 14, 2017 - 20:22:40
Django version 1.10.5, using settings 'config.settings.local'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

对此行为的任何帮助或解释都将不胜感激。

1 个答案:

答案 0 :(得分:0)

.env文件旨在与Docker一起使用,但它是一个好主意!

我正在使用Apache和&#39; .env&#39;运行Debian服务器。文件不起作用,相反,我必须继续使用“秘密”文件模式&#39;它将这些变量存储在json文件中。如果我真的想要&#39; runserver&#39;使用&#39; .env&#39;文件,在我的系统之前没有设置任何环境变量,我必须在base.py设置中将以下内容设置为&#39; True&#39;:

READ_DOT_ENV_FILE = env.bool('DJANGO_READ_DOT_ENV_FILE', default=True)

我不确定但是,上面超过了DJANGO_READ_DOT_ENV_FILE&#39;通知&#39; .env&#39;文件。所以,你必须设置&#39; True&#39;直接在&#39; base.py&#39;然后可以通过manage.py直接加载您的设置:

$ python manage.py runserver --settings=config.settings.local
or
$ python manage.py runserver --settings=config.settings.production