Unable to access Heroku config vars from Django settings.py

泄露秘密 提交于 2019-12-10 12:08:08

问题


I have some config vars set on Heroku as described here: https://devcenter.heroku.com/articles/config-vars. When I run:

heroku config

I see the config var in the list:

COMPRESS_OFFLINE:                  True

and I can run

heroku config:get COMPRESS_OFFLINE

and I get:

True

In my Django settings.py, I have:

import os
COMPRESS_OFFLINE = os.environ['COMPRESS_OFFLINE']

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?


回答1:


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

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)

So that it defaults to True during compile when the environment variable is not available.



来源:https://stackoverflow.com/questions/21683846/unable-to-access-heroku-config-vars-from-django-settings-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!