So I made a thing for using environment variables within your Django settings: django-getenv.
Although the code itself is trivial (and, to be honest, not coupled with Django in any way), because I was re-using the same code in multiple projects I decided – if only to make my life easier – to made it into a standalone module and package.
Enough of the what, let us get onto the why.
I’m a big fan of The Twelve-Factor App, and although I can’t or don’t always follow its tenets in every app I write, I do my best.
One of the tenets is “Store config in the environment”:
The twelve-factor app stores config in environment variables (often shortened to
env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.
Although Django has a range of configuration options, it does not lend itself well to using environment variables out of the box.
django-getenv is another piece of the puzzle, helping you use those environment variables in your Django project settings. As well as simplifying accessing these variables, it will also convert boolean, integer and float values to their native Python types.