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 envvar is required (where project_name is the module originally generated by startproject):

export DJANGO_PROJECT=project_name

The following envvars are also required (if settings.py is removed):

  • ALLOWED_HOSTS (unless you set DEBUG=on)
  • DATABASES or PGDATABASE

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.

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

Project details


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.1.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

django_envconfig-0.1.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-envconfig-0.1.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django-envconfig-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a4841459a66e930c3084e67096c0bfc64fa34d4bb9d5978410d101d467c02087
MD5 a83e705c76946793c202f5b385f25cf7
BLAKE2b-256 f2d6c216ad11da9fdcd1006decebeee03996d1cd319eb815f3b5a91bdfb296b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_envconfig-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django_envconfig-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b9791a6a8d2c470be1dc94886c53523aa80fb7deafd6cecc1a217f4ebb284cd
MD5 d535d27b32eb1de0c4271ee889b698cb
BLAKE2b-256 88c40f6ba32edf6d326f78595fdb53bb3126de5c7b864a198aa291963d4fd516

See more details on using hashes here.

Supported by

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