Where do I set environment variables for Django?

前端 未结 4 1734
小鲜肉
小鲜肉 2020-12-09 11:11

everyone!

Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04 in AWS

I want to set environment variables for sensitive info.(django secret key, DB passwor

4条回答
  •  温柔的废话
    2020-12-09 11:20

    The simplest solution is as already mentioned using os.environ.get and then set your server environment variables in some way (config stores, bash files, etc.)

    Another slightly more sophisticated way is to use python-decouple and .env files. Here's a quick how-to:

    1) Install python-decouple (preferably in a venv if not using Docker):

    pip install python-decouple
    

    2) Create a .env file in the root of your Django-project, add a key like;

    SECRET_KEY=SomeSecretKeyHere
    

    3) In your settings.py, or any other file where you want to use the configuration values:

    from decouple import config
    
    ...
    
    SECRET_KEY = config('SECRET_KEY')
    

    4) As you probably don't want these secrets to end up in your version control system, add the file to your .gitignore. To make it easier to setup a new project, you could have a .env_default checked into the VCS containing default/dummy-values that's not used in production.

提交回复
热议问题