Skip to main content

Django-environ allows you to utilize 12factor inspired environmentvariables 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

How to install

::

$ pip install django-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.1 (2013-04-19) ===

  • environ/environ.py: Env.__call__ now uses Env.get_value instance method

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

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-environ-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c3ca1448f6a1ddf96ae446887574c998d7f8dc23570510e9bd18e3894bf62af7
MD5 c8f80f2c2d8d9aeec64308008f334e9e
BLAKE2b-256 0f71ceb58b6ccb4459c324ff9f84c3cd721e264ac2af08139a780b5ff7a05632

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