Hiding settings.py passwords for Heroku Django deployment

眉间皱痕 提交于 2021-02-18 07:51:41

问题


I have sensitive data (database passwords) in settings.py and I was advised to upload my Django project to a github repository before pushing it to Heroku on their "Getting Started with Django on Heroku". If I put settings.py in .gitignore, then presumably it won't get deployed with my project. How can I prevent settings.py from being exposed but still get it deployed with my project ?


回答1:


You can use environment variables (with heroku config:add SECRET=my-secret) to setup sensitive data and retrieve them in your settings with:

SECRET = os.environ.get('SECRET', 'my-default-secret-key')

If you don't want to be able to start your app without having set up some data, use this syntax instead:

SECRET = os.environ['SECRET']

It will raise an exception if you didn't set the SECRET environment variable.




回答2:


You should use a tool designed for factoring out sensitive data. I use YamJam https://pypi.python.org/pypi/yamjam/ . It allows all the advantages of the os.environ method but is simpler -- you still have to set those environ variables, you'll need to put them in a script/ rc file somewhere. YamJam eliminates these questions and stores these config settings in a config store outside of the project. This allows you to have different settings for dev, staging and production.

  from YamJam import yamjam

  secret = yamjam()['myproject']['secret']

Is the basic usage. And like the os.environ method, it is not framework specific, you can use it with Django or any other app/framework. I've tried them all, multiple settings.py files, brittle logic of if/then and environment wrangling. In the end, I switched to yamjam and haven't regretted it.



来源:https://stackoverflow.com/questions/19071977/hiding-settings-py-passwords-for-heroku-django-deployment

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