无法从Django settings.py访问Heroku config vars [英] Unable to access Heroku config vars from Django settings.py
问题描述
我在Heroku上设置了一些config vars,如下所述: https://devcenter.heroku。 COM /用品/配置 - 瓦尔。
当我运行时:
I have some config vars set on Heroku as described here: https://devcenter.heroku.com/articles/config-vars. When I run:
heroku config
我看到列表中的config var:
I see the config var in the list:
COMPRESS_OFFLINE: True
我可以运行
heroku config:get COMPRESS_OFFLINE
我得到:
True
在我的Django settings.py,我有:
In my Django settings.py, I have:
import os
COMPRESS_OFFLINE = os.environ['COMPRESS_OFFLINE']
这是上面链接的文章告诉您访问Django中的Heroku配置变量。我在本地的.env文件中有这个config var set,我可以在本地运行工头。但是,当我部署到Heroku时,在部署过程中会收到此错误:
This is how the article linked above tells you to access Heroku config variables in Django. I have this config var set in my local .env file, and I can run foreman locally. But, when I deploy to Heroku, I get this error during deploy:
KeyError: 'COMPRESS_OFFLINE'
! Push rejected, failed to compile Python app
我有这种感觉,我很遗憾,建议?
I have the feeling I'm missing something painfully obvious, any suggestions?
推荐答案
我从Heroku的支持中得到一个答案,默认情况下,在slug编译期间环境变量不可用。但是,有一个名为user-env-compile的实验功能将使它们可用: http://devcenter.heroku.com/articles/labs-user-env-compile
I got an answer back from Heroku support that environment variables are not available during slug compilation by default. But, there is an experimental feature called user-env-compile that will make them available: http://devcenter.heroku.com/articles/labs-user-env-compile
然而,我不需要这个。我以为是因为在编译过程中找不到它们,根本无法找到它们。但是,我发现编译后可以使用它们。所以,我刚刚更新了settings.py,以便:
I didn't need this, though. I thought that because it couldn't find them during compile that it wouldn't be able to find them at all. But, I found that they are available after compile. So, I just updated settings.py to have:
COMPRESS_OFFLINE = os.environ.get('COMPRESS_OFFLINE', True)
当环境变量不可用时,它在编译时默认为True。
So that it defaults to True during compile when the environment variable is not available.
这篇关于无法从Django settings.py访问Heroku config vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!