Skip to main content

django-envconfig

Test Coverage Version

Django Python

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 set DEBUG=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 and false|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):

  1. Environment variables.
  2. Settings defined in your projects settings.py, if it exists. Note: any custom settings should be defined here with their default value.
  3. Settings that would be defined by a settings.py file generated by startproject. This should eliminate the need for the file in (2) for most projects. Caveats:
    • The default value for DEBUG has been changed to False.
    • 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 persistent SECRET_KEY as an environment variable.

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

License

MIT.

Metadata

Release files for django-envconfig 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-envconfig 0.4.0
File Size Uploaded
django_envconfig-0.4.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-envconfig 0.4.0
File Interpreter ABI Platform
django_envconfig-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / django_envconfig-0.4.0.tar.gz

Download URL django_envconfig-0.4.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6fba8d758a078c5022963986ff0bd34bbdb10a9f1b291f680d517579445ef2cb
BLAKE2b-256 checksum
How to use checksums
05fbb2add9ed34993ca901a6609cebad693e35dfb3d190201082cf2343587f4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / django_envconfig-0.4.0-py3-none-any.whl

Download URL django_envconfig-0.4.0-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38e07ab8beda22e5548ec3c33f2f0109f482af9afed84f11d42b75bb58f4edd7
BLAKE2b-256 checksum
How to use checksums
a8d7724d45d17e70c03a0ef4a0d71b7b18204d3ea6e2cdfa246d432095863095
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page