docs: https://django-environ.readthedocs.io/en/latest/
install:
pip install django-environ
NOTE: No need to add it to INSTALLED_APPS.
Then create a .env
file to project path:
.env
then settings.py:
import environ # initialize env env = environ.Env( # set casting, default value DEBUG=(bool, False) ) # reading .env file environ.Env.read_env() # False if not in os.environ DEBUG = env('DEBUG') # Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ SECRET_KEY = env('SECRET_KEY') # Parse database connection url strings like psql://user:[email protected]:8458/db DATABASES = { # read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found 'default': env.db(), # read os.environ['SQLITE_URL'] 'extra': env.db('SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db') } CACHES = { # read os.environ['CACHE_URL'] and raises ImproperlyConfigured exception if not found 'default': env.cache(), # read os.environ['REDIS_URL'] 'redis': env.cache('REDIS_URL') }