Package: django-envconf
Django EnvConf allows you to configure your application using environment variables
as recommended by the 12factor methodology.Shamelessly forked & updated fromhttps://github.com/joke2k/django-environQuick startAdd “envconf” at the top of yoursettings.pyfile like so:fromenvconfimportEnvenv=Env(# Set default values and castingDEBUG=(bool,False))env.read_env()# Tries to read the `.env` file which is next to the `manage.py` script.# It's probably better to give the path to be sure it'll read the correct file.Create a.envfile at the root of your projectDEBUG=on# or off / false
# DJANGO_SETTINGS_MODULE=myapp.settings.devSECRET_KEY=Tom-Marvolo-RiddleDATABASE_URL=psql://user:un-gitted-password@127.0.0.1:8458/database# DATABASE_URL=sqlite:////my-local-sqlite.db  # sqlite, notice the 4 slashes. See below for more cases.CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213REDIS_URL=rediscache://127.0.0.1:6379:1?client_class=django_redis.client.DefaultClient&password=un-gitted-passwordThen fetch the variable you want from the environment in yoursettings.pyfile:DEBUG=env('DEBUG')# Defaults to FalseSECRET_KEY=env('SECRET_KEY')# Raises ImproperlyConfigured exception if SECRET_KEY is not setDATABASES={'default':env.db(),# Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ'extra':env.db('SQLITE_URL',default='sqlite:////tmp/my-tmp-sqlite.db')}InstallationThrough Pypi(venv)$pipinstalldjango-envconfDirectly from git(venv)$pipinstallgit+https://github.com/achedeuzot/django-envconf.git# or(venv)$gitclonehttps://github.com/achedeuzot/django-envconf.git&&cddjango-envconf(venv)$pythonsetup.pyinstallUsageIn your settings or configuration module, first either import the standard parser or a Django schema:# DefaultfromenvconfimportEnvenv=Env()# Schemasfromenvconf.schemas.django110importDjango110Envasenvenv('DEBUG')# defaults to False# Defaults with the following:# DEBUG bool# SECRET_KEY str# DATABASES extracted from DATABASE_URL to dict()envcan be called two ways:Type explicit:env('VAR_NAME',cast=bool)Type implicit (see below for supported types):env.TYPE('ANOTHER_VAR'). If type is not specified, it defaults
tostrCasting explicitly:# Environment variable: MAIL_ENABLED=1mail_enabled=env('MAIL_ENABLED',cast=bool)# OR mail_enabled = env.bool('MAIL_ENABLED')assertmail_enabledisTrueCasting nested types (lists and dicts):# Environment variable: FOO=1,2,3foo=env('FOO'),cast=list(int))assertfoo==[1,2,3]You can also set defaults:# Environment variable MAX_ROWS has not been definedmax_rows=env.int('MAX_ROWS',default=100)assertmax_rows==100There are some convenience methods:
- json (a regular JSON string is expected)
- url (which returns aurlparse.ParseResultobject)# Environment variable: DATA={"foo":"bar","baz":true}data=env.json('DATA')# data = {#   "foo": "bar",#   "baz": True,# }# Environment variable: SERVICE=ftp://user:password@example.com/some/path?var=foo>>>env.url('SERVICE')ParseResult(scheme='ftp',netloc='user:password@example.com',path='/some/path',params='',query='var=foo',fragment='')Proxied ValuesAn environment value or default can reference another environ value by referring to it with a $ sign.  For example:PROXIED_VAL='hello'TEST_VAL='$PROXIED_VAL'environ('TEST_VAL')=='helloenviron('UNKNOWN_VAL',default='$PROXIED_VAL')=='hello'Proxy values are resolved by default.  To turn off resolving proxy values
passresolve_proxies=Falsetoenviron,environ.str, orenviron.unicode.Ex:environ('DJANGO_SECRET_KEY','$1233FJSIFWR44', resolve_proxies=False)If you get an infinite recursion when using environ most likely you have an unresolved and perhaps
unintentional proxy value in an environ string.
For exampleenviron('DJANGO_SECRET_KEY','$1233FJSIFWR44')will cause an infinite
recursion unless you addresolve_proxies=False.This is very useful in environment such as Heroku. That way, if you
change your mind later on, you just need to change the configuration (see below) and not your code.# Environment variables: MAILGUN_SMTP_LOGIN=foo,# SMTP_LOGIN='$MAILGUN_SMTP_LOGIN'smtp_login=env('SMTP_LOGIN')assertsmtp_login=='foo'# Change of mind# Environment variales: MANDRILL_SMTP_LOGIN=bar# SMTP_LOGIN='$MANDRILL_SMTP_LOGIN'smtp_login=env('SMTP_LOGIN)  # Look ma',nohands!assertsmtp_login=='bar'Supported Typesstrboolintfloatjsonlist as CSV (FOO=a,b,c)tuple (FOO=(a,b,c))dict (dict (BAR=key=val,foo=bar)  # envconf.Env(BAR=(dict, {}))dict (BAR=key=val;foo=1.1;baz=True)  # envconf.Env(BAR=(dict(value=unicode, cast=dict(foo=float,baz=bool)), {}))urlpath (environ.Path)db_urlPostgreSQL: postgres://, pgsql://, psql:// or postgresql://PostGIS: postgis://MySQL: mysql:// or mysql2://MySQL for GeoDjango: mysqlgis://SQLITE: sqlite:// (sqlite://:memory: for in-memory database, or sqlite:////file/path [4 slashes !])SQLITE with SPATIALITE for GeoDjango: spatialite://Oracle: oracle://LDAP:ldap://cache_urlDummy: dummycache://Database: dbcache://File: filecache://Memory: locmemcache://Memcached: memcache://Python memory: pymemcache://Redis: rediscache://search_urlElasticSearch: elasticsearch://Solr: solr://Whoosh: whoosh://Xapian: xapian://Simple cache: simple://email_urlDummy mail: dummymail://SMTP: smtp://SMTP+SSL: smtp+ssl://SMTP+TLS: smtp+tls://Console mail: consolemail://File mail: filemail://LocMem mail: memorymail://TestsClone the repo and run the tests ;)(venv)$gitclonegit@github.com/achedeuzot/django-envconf.git(venv)$cddjango-envconf(venv)$pythonsetup.pytestLicenseDjango-envconf is licensed under the BSD License - see the LICENSE file for detailsCompatibilityPython 2.6, 2.7, 3.3, 3.4, 3.5Django 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10Creditsdjango-environand its contributors & own creditsof course ! Thanks for the awesome package :)Changelog0.1.0, 0.2.0, 0.3.* - 12 Sept 2016Fork fromdjango_environand update of codebase: removal of six dependencly, better oracle support,
better URL parsing