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

Credits

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-environ-0.2.tar.gz (10.4 kB view details)

Uploaded Source

File details

Details for the file django-environ-0.2.tar.gz.

File metadata

  • Download URL: django-environ-0.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-environ-0.2.tar.gz
Algorithm Hash digest
SHA256 57468a96642dac540eb759aed544949c83f07a8a986919340179cf85809d163b
MD5 15076d05dfba9280582c6bc747684a06
BLAKE2b-256 1c57c63561aabe782ef11eeea4cfd0b5e7625e7672889eb38baeea9f79010c5c

See more details on using hashes here.

Supported by

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