Skip to main content

Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Project description

Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application. This is your settings.py file before you have installed django-envronment:

import os
SITE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database',
        'USER': 'user',
        'PASSWORD': 'githubbedpassword',
        'HOST': '123.123.123.123',
        'PORT': '8458',
    }
    'extra': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(SITE_ROOT, 'database.sqlite')
    }
}

MEDIA_ROOT = os.path.join(SITE_ROOT, 'assets')
MEDIA_URL = 'media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = 'static/'

SECRET_KEY = '...im incredibly still here...'

After:

import environ
root = environ.Path(__file__) - 3 # three folder back (/a/b/c/ - 3 = /)
env = environ.Env(DEBUG=(bool, False),) # set default values and casting

SITE_ROOT = root()

DEBUG = env('DEBUG') # False if not in os.environ
TEMPLATE_DEBUG = DEBUG

DATABASES = {
    'default': env.db(), # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    'extra': env.db('SQLITE_DB_URL', default=root('database.sqlite'))
}

MEDIA_ROOT = root('assets')
MEDIA_URL = 'media/'
STATIC_ROOT = root('static')
STATIC_URL = 'static/'

SECRET_KEY = env('SECRET_KEY') # Raises ImproperlyConfigured exception if SECRET_KEY not in os.environ

DevMode

Put your environment variables definition in a .env file:

$ cat >.env <<EOM
DEBUG=on
SECRET_KEY=1110110010111101111011101111
DATABASE_URL=postgres://uf07k1:wegauwhg@compute-1.amazonaws.com:5431/d8r827
SQLITE_URL=sqlite:////var/db/myapp.db
EOM

Then in your settings.py or settings/dev.py:

import environ
root = environ.Path(__file__) - 3
env = environ.Env()

env.read_env(root('.env'))

...

Tests

$ git clone git@github.com:joke2k/django-environ.git
$ cd django-environ/
$ python setup.py test

Changelog

=== 0.2 (2013-04-16) ===

  • environ/environ.py, environ/test.py, environ/test_env.txt: add advanced float parsing (comma and dot symbols to separate thousands and decimals)
  • README.rst, docs/index.rst: fix TYPO in documentation

=== 0.1 (2013-04-02) ===

  • initial release

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django-environ-0.2.tar.gz (10.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page