Package: django-environ-docker
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.This module is a merge of:envparsehonchodj-database-urldj-search-urldj-config-urldjango-cache-urland inspired by:12factor12factor-djangoTwo Scoops of DjangoThis is yoursettings.pyfile before you have installeddjango-environimportosSITE_ROOT=os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))DEBUG=TrueTEMPLATE_DEBUG=DEBUGDATABASES={'default':{'ENGINE':'django.db.backends.postgresql_psycopg2','NAME':'database','USER':'user','PASSWORD':'githubbedpassword','HOST':'127.0.0.1','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...'CACHES={'default':{'BACKEND':'django.core.cache.backends.memcached.MemcachedCache','LOCATION':['127.0.0.1:11211','127.0.0.1:11212','127.0.0.1:11213',]},'redis':{'BACKEND':'django_redis.cache.RedisCache','LOCATION':'127.0.0.1:6379/1','OPTIONS':{'CLIENT_CLASS':'django_redis.client.DefaultClient','PASSWORD':'redis-githubbed-password',}}}After:importenvironroot=environ.Path(__file__)-3# three folder back (/a/b/c/ - 3 = /)env=environ.Env(DEBUG=(bool,False),)# set default values and castingenviron.Env.read_env()# reading .env fileSITE_ROOT=root()DEBUG=env('DEBUG')# False if not in os.environTEMPLATE_DEBUG=DEBUGDATABASES={'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')}public_root=root.path('public/')MEDIA_ROOT=public_root('media')MEDIA_URL='media/'STATIC_ROOT=public_root('static')STATIC_URL='static/'SECRET_KEY=env('SECRET_KEY')# Raises ImproperlyConfigured exception if SECRET_KEY not in os.environCACHES={'default':env.cache(),'redis':env.cache('REDIS_URL')}You can also passread_env()an explicit path to the.envfile.Create a.envfile:DEBUG=on# DJANGO_SETTINGS_MODULE=myapp.settings.devSECRET_KEY=your-secret-keyDATABASE_URL=psql://urser:un-githubbedpassword@127.0.0.1:8458/database# SQLITE_URL=sqlite:///my-local-sqlite.dbCACHE_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=redis-un-githubbed-passwordHow to install$ pip install django-environHow to useThere are only two classes,environ.Envandenviron.Path>>>importenviron>>>env=environ.Env(DEBUG=(bool,False),)>>>env('DEBUG')False>>>env('DEBUG',default=True)True>>>open('.myenv','a').write('DEBUG=on')>>>environ.Env.read_env('.myenv')# or env.read_env('.myenv')>>>env('DEBUG')True>>>open('.myenv','a').write('\nINT_VAR=1010')>>>env.int('INT_VAR'),env.str('INT_VAR')1010,'1010'>>>open('.myenv','a').write('\nDATABASE_URL=sqlite:///my-local-sqlite.db')>>>env.read_env('.myenv')>>>env.db(){'ENGINE':'django.db.backends.sqlite3','NAME':'my-local-sqlite.db','HOST':'','USER':'','PASSWORD':'','PORT':''}>>>root=env.path('/home/myproject/')>>>root('static')'/home/myproject/static'Seecookiecutter-djangofor a concrete example on using with a django project.Supported Typesstrboolintfloatjsonlist (FOO=a,b,c)tuple (FOO=(a,b,c))dict (BAR=key=val,foo=bar) #environ.Env(BAR=(dict, {}))dict (BAR=key=val;foo=1.1;baz=True) #environ.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 with SPATIALITE for GeoDjango: spatialite://Oracle: oracle://Redshift: redshift://LDAP:ldap://cache_urlDatabase: dbcache://Dummy: dummycache://File: filecache://Memory: locmemcache://Memcached: memcache://Python memory: pymemcache://Redis: rediscache://search_urlElasticSearch: elasticsearch://Solr: solr://Whoosh: whoosh://Xapian: xapian://Simple cache: simple://email_urlSMTP: smtp://SMTP+SSL: smtp+ssl://SMTP+TLS: smtp+tls://Console mail: consolemail://File mail: filemail://LocMem mail: memorymail://Dummy mail: dummymail://TipsUsing unsafe characters in URLsIn order to use unsafe characters you have to encode withurllib.parse.encodebefore you set into.envfile.DATABASE_URL=mysql://user:%23password@127.0.0.1:3306/dbnameSeehttps://perishablepress.com/stop-using-unsafe-characters-in-urls/for reference.Email settingsIn order to set email configuration for django you can use this code:EMAIL_CONFIG=env.email_url('EMAIL_URL',default='smtp://user@:password@localhost:25')vars().update(EMAIL_CONFIG)SQLite urlsSQLite connects to file based databases. The same URL format is used, omitting the hostname,
and using the “file” portion as the filename of the database.
This has the effect of four slashes being present for an absolute
file path: sqlite:////full/path/to/your/database/file.sqlite.Tests$ git clone git@github.com:joke2k/django-environ.git
$ cd django-environ/
$ python setup.py testLicenseDjango-environ is licensed under the MIT License - see theLICENSE_FILEfile for detailsChangelog0.4.2 - 13-April-2017Confirm support for Django 1.11.Support for Redshift database URLFix uwsgi settings reload problem (#55)Update support for django-redis urls (#109)0.4.1 - 13-November-2016Fix for unsafe characters into URLsClarifying warning on missing or unreadable file. Thanks to @nickcatalAdd support for Django 1.10.Fix support for Oracle urlsFix support for django-redis0.4.0 - 23-September-2015Fix non-ascii values (broken in Python 2.x)New email schemes - smtp+ssl and smtp+tls (smtps would be deprecated)redis_cache replaced by django_redisAdd tuple support. Thanks to @anonymouzzAdd LDAP url support for database (django-ldapdb)Fix psql/pgsql url0.3 - 03-June-2014Add cache url supportAdd email url supportAdd search url supportRewriting README.rst0.2.1 19-April-2013environ/environ.py: Env.__call__ now uses Env.get_value instance method0.2 16-April-2013environ/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 documentation0.1 02-April-2013initial releaseCredits12factor12factor-djangoTwo Scoops of Djangorconradharris/envparsekennethreitz/dj-database-urlmigonzalvar/dj-email-urlghickman/django-cache-urldstufft/dj-search-urljulianwachholz/dj-config-urlnickstenning/honchoenvparseDistributemodern-package-template