django+environ

1.配置数据库连接在环境配置中

新建  .env
DEBUG=True
SECRET_KEY=your-secret-key
DATABASE_URL=mysql://urser:[email protected]:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret

2.在settings.py 中配置

import environ

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
environ.Env.read_env(".env") 
#如果找不到可以设置·位置 例如 /home/source/xx.env
DEBUG = env('DEBUG')
SECRET_KEY = env('SECRET_KEY')


DATABASES = {
    'default': env.db(),
}
CACHES = {
    # read os.environ['CACHE_URL'] and raises ImproperlyConfigured exception if not found
    'default': env.cache(),
    # read os.environ['REDIS_URL']
    'redis': env.cache('REDIS_URL')
}

你可能感兴趣的:(django+environ)