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=(False,bool),) # 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

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for django-environ-0.1.tar.gz
Algorithm Hash digest
SHA256 9d781d3cd7fd4234289828431993a76b2f523ec0d797e14bdcee080452b510cf
MD5 f1b82707548e8c748a8e75ceec1aac55
BLAKE2b-256 b6cf6491990ec0c13fa1a23397b5127c80168c888028084d108c4b928b50fd9c

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