In trying to find a place to store and save settings beyond settings.py and the database, I used an environment.json for environment variables. I import these in settings.py.
You might want to look into foreman
(GitHub) or honcho
(GitHub). Both of these look for a .env
file in your current directory from which to load local environment variables.
My .env
looks like this for most projects (I use dj-database-url for database configuration):
DATABASE_URL=sqlite://localhost/local.db
SECRET_KEY=
DEBUG=True
In your settings.py
file, you can load these settings from os.environ
like this:
import os
DEBUG = os.environ.get('DEBUG', False)
If there are required settings, you can assert
their presence before trying to set them:
assert 'SECRET_KEY' in os.environ, 'Set SECRET_KEY in your .env file!'
SECRET_KEY = os.environ['SECRET_KEY']
I've been using this method of handling local settings for the last few projects I've started and I think it works really well. One caveat is to never commit your .env
to source control. These are local settings that exist only for the current configuration and should be recreated for a different environment.