Skip to main content

Configure Django using environment variables.

Project description

django-envconfig

Test Python Django License

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).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-envconfig-0.2.6.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

django_envconfig-0.2.6-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file django-envconfig-0.2.6.tar.gz.

File metadata

  • Download URL: django-envconfig-0.2.6.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for django-envconfig-0.2.6.tar.gz
Algorithm Hash digest
SHA256 d6512530be2e6ee08fe7791bc7e1969f1f499b35637c0db39b325ed0d00ef63c
MD5 4c46f6f0b35d2bbc707d3400196b30c7
BLAKE2b-256 33faac83bdc3c09b97f290b7f8bc5bfcd9407d960b33907f67d95d9a9b2607a8

See more details on using hashes here.

File details

Details for the file django_envconfig-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_envconfig-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 52992e5754a9617e4d336ed03d5df15832e11dc4db01dab4283bcf96635ff37a
MD5 60b7c6ef1d83c14e11ef174deec535b4
BLAKE2b-256 33ecb48e2c4f7255184bc5f7e58037cf7f06a56eca788fa0a0d163f7a67a7686

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page