Configure Django using environment variables.
Project description
django-envconfig
Configure Django using environment variables (envvars). settings.py
optional.
Getting started
Installation
python -m pip install django-envconfig
Usage
Edit the manage.py
, asgi.py
and wsgi.py
files generated by Django's
startproject
command and modify the following line:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'envconfig.settings')
Minimum configuration
The following envvars are required (if settings.py
is removed):
ALLOWED_HOSTS
(unless you setDEBUG=on
)
Environments may be stored in an .env
file. This file can be stored in your
root directory (next to manage.py
) or anywhere on the path (e.g. virtualenv
directory).
How it works
Any Django setting can be configured as an environment variable.
- To set booleans:
true|yes|on|1
andfalse|no|off|0
(case-insensitive) - To set
None
:none|null
(case-insensitive) - Simple lists of strings can be stored comma-separated e.g.
export ALLOWED_HOSTS=127.0.0.1,localhost
- Dicts and complex lists should be stored as JSON
Settings are loaded with the following priority (highest first):
- Environment variables.
- Settings defined in your projects
settings.py
, if it exists. Note: any custom settings should be defined here with their default value. - Settings that would be defined by a
settings.py
file generated bystartproject
. This should eliminate the need for the file in (2) for most projects. Caveats:- The default value for
DEBUG
has been changed toFalse
. - A
SECRET_KEY
is generated but will not persist between sessions (e.g. if you restart your server/process manager). Check the Django documentation to see whether you need to set a persistentSECRET_KEY
as an environment variable.
- The default value for
django-envconfig environment variables
Helper environment variables to use with django-envconfig:
Environment variable | Description |
---|---|
DJANGO_PROJECT |
May be required if django-envconfig cannot find your project. Set to the name of the module originally generated by startproject |
ADD_INSTALLED_APPS |
Add to INSTALLED_APPS |
REMOVE_INSTALLED_APPS |
Remove from INSTALLED_APPS |
ADD_MIDDLEWARE |
Add to MIDDLEWARE |
REMOVE_MIDDLEWARE |
Remove from MIDDLEWARE |
PostgreSQL environment variables
If you are using a PostgreSQL backend you do not need to set DATABASES
. You
can simply set PostgreSQL environment variables - the minimum is PGDATABASE
.
See the
PostgreSQL docs
for the full list of envvars. This way the same environment can be used when
calling
PostgreSQL command line utilities
such as psql
or pg_dump
.
Why
- To separate configuration from code. See The Twelve Factor App.
- Use serverless services such as AWS Lambda and Heroku.
- Avoid having to template settings files and keep the auto-generated
settings.py
up to date between Django versions. - Use .env files for easy switching between environments/deployments (e.g. dev, test and prod).
Dependencies
- python-dotenv (BSD 3-clause license)
License
MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-envconfig-0.3.1.tar.gz
.
File metadata
- Download URL: django-envconfig-0.3.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c07b98ee0e1baca794d04ed484fdd13e7b1098b2621dfdb0170fd155263e959e |
|
MD5 | 058c75b893113835c70120cd0cc19df9 |
|
BLAKE2b-256 | e96d1fc8062bc6dcf99acb1863b7db6d8c8b492a2ed8bed92897bba4deadea11 |
File details
Details for the file django_envconfig-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: django_envconfig-0.3.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f04bbc4e2b1b850d5b45475270779997b2a004af4a533e11d8d9e861c8b3578a |
|
MD5 | 70ace5a3cb465dbd95a6a2d28fdc6a6d |
|
BLAKE2b-256 | db8e30c3d50a3946ff83d8e7b5382733cf6640cbb6ac96821346d6d91e023664 |