django-environ学习

【django-environ学习】官方说明:https://django-environ.readthedocs.io/en/latest/index.html
installpip install django-environquick startimport environimport osenv = environ.Env(# set casting, default valueDEBUG=(bool, False))# Set the project base directoryBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Take environment variables from .env fileenviron.Env.read_env(os.path.join(BASE_DIR, '.env'))# False if not in os.environ because of casting aboveDEBUG = env('DJANGO_DEBUG')# Raises Django's ImproperlyConfigured# exception if SECRET_KEY not in os.environSECRET_KEY = env('DJANGO_SECRET_KEY')# Parse database connection url strings# like psql://user:pass@127.0.0.1:8458/dbDATABASES = {# read os.environ['DATABASE_URL'] and raises# ImproperlyConfigured exception if not found## The db() method is an alias for db_url().'default': env.db(),# read os.environ['SQLITE_URL']'extra': env.db_url('SQLITE_URL',default='sqlite:////tmp/my-tmp-sqlite.db')}CACHES = {# Read os.environ['CACHE_URL'] and raises# ImproperlyConfigured exception if not found.## The cache() method is an alias for cache_url().'default': env.cache(),# read os.environ['REDIS_URL']'redis': env.cache_url('REDIS_URL')}.envDJANGO_DEBUG=TrueDJANGO_SECRET_KEY=your-secret-keyDATABASE_URL=psql://user:un-githubbedpassword@127.0.0.1:8458/databaseSQLITE_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=ungithubbed-secret

    推荐阅读