Skip to main content

Configure Django using environment variables.

Project description

django-envconfig

Test Python Django License

Configure Django using environment variables. Having a settings.py file is 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 change the following line:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'envconfig.settings')

Minimum configuration

The following environment variable is required:

export DJANGO_PROJECT=my_project_name

In the absence of a settings.py file the following environment variables are required: ALLOWED_HOSTS and either DATABASES or PGDATABASE.

Environments may be stored in an .env file. This file can be stored in your root directory (same as 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 a boolean: 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.
  3. Settings that would be defined by a settings.py file generated by startproject. This should eliminate the need for the file in most projects. Two main 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). This may or may not be an issue depending on your use case - see the Django documentation for more details.

If you are using a PostgreSQL backend you do not need to specify a DATABASES setting if you have set PostgreSQL environment variables - the minimum is PGDATABASE. See the PostgreSQL docs for more details.

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.1.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

django_envconfig-0.1.1-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

Supported by

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