I have a Django project running on Ubuntu 16.04 with Nginx and Gunicorn. I am trying to set an environment variable in order to load a different settings file for production. I have tried the following to no prevail.
我有一个在Nbux和Gunicorn上运行在Ubuntu 16.04上的Django项目。我正在尝试设置一个环境变量,以便为生产加载不同的设置文件。我尝试过以下几点都没有占上风。
In etc/environment I added my variable. The file looks like this,
在etc / environment中我添加了我的变量。该文件看起来像这样,
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
PRODUCTION="Production_Variable"
The value of the variable is irrelevant.
变量的值是无关紧要的。
When I run printenv I can see that this variable is actually present! However my django project still does not "see" it.
当我运行printenv时,我可以看到这个变量实际存在!但是我的django项目仍然没有“看到”它。
try:
if 'PRODUCTION' in os.environ:
from .prod import *
except:
pass
I am a bit confused that printenv shows that the environment variable is present, yet Django cannot see it. Does anything look blatantly wrong with what I am attempting?
我有点困惑,printenv显示环境变量存在,但Django无法看到它。我试图做什么看起来有什么明显的错误?
2 个解决方案
#1
0
Okay, I hope this helps someone in the future who is running into the same problem. I ended up editing my gunicorn.service file and adding the following line..
好的,我希望这可以帮助将来遇到同样问题的人。我最终编辑了我的gunicorn.service文件并添加了以下行..
Environment="Production=production"
So my file looks like this
所以我的文件看起来像这样
[Unit]
Description=gunicorn daemon
After= network.target
[Service]
User=root
Group=www-data
Environment="Production=production"
WorkingDirectory= # stuff
[Install]
WantedBy=multi-user.target
#2
-1
Have a look at django own settings file.
看看django自己的设置文件。
This part might be helpful
这部分可能会有所帮助
add the custom env variable in settings file:
在设置文件中添加自定义env变量:
MY_CUSTOM_SETTINGS = 'hello'
then you can access it using:
然后您可以使用以下方式访问它
from django.conf import settings
In [5]: print(settings.MY_CUSTOM_SETTINGS)
hello
#1
0
Okay, I hope this helps someone in the future who is running into the same problem. I ended up editing my gunicorn.service file and adding the following line..
好的,我希望这可以帮助将来遇到同样问题的人。我最终编辑了我的gunicorn.service文件并添加了以下行..
Environment="Production=production"
So my file looks like this
所以我的文件看起来像这样
[Unit]
Description=gunicorn daemon
After= network.target
[Service]
User=root
Group=www-data
Environment="Production=production"
WorkingDirectory= # stuff
[Install]
WantedBy=multi-user.target
#2
-1
Have a look at django own settings file.
看看django自己的设置文件。
This part might be helpful
这部分可能会有所帮助
add the custom env variable in settings file:
在设置文件中添加自定义env变量:
MY_CUSTOM_SETTINGS = 'hello'
then you can access it using:
然后您可以使用以下方式访问它
from django.conf import settings
In [5]: print(settings.MY_CUSTOM_SETTINGS)
hello