Django: Deployment

 20th August 2020 at 2:19pm

Environment-Specific Variables

根据 12-factor,环境相关的变量都应该从环境中读取。django-environ 可以做这个事情,比如下面的例子,从运行环境的 .env 文件读取变量:

# `environ' is from django-environ
import environ

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
env.read_env('.env')
DEBUG = env('DEBUG')
REDIS_URL = env('REDIS_URL')
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {"hosts": [REDIS_URL]},
    }
}
DATABASES = {
    "default": env.db()
}
EMAIL_CONFIG = env.email_url('EMAIL_URL')

.env 理所当然不应该被上传到代码仓库。