
项目/config/__init__.py
# -*- coding: utf-8 -*-# 本地开发环境RUN_MODE = "DEVELOP"# APP本地静态资源目录STATIC_URL = "/static/"# APP静态资源目录url# REMOTE_STATIC_URL = "%sremote/" % STATIC_URLDEBUG = True# 本地开发数据库设置DATABASES = {"default": {"ENGINE": "django.db.backends.mysql","NAME": "dbname", # noqa"USER": "root","PASSWORD": "root","HOST": "127.0.0.1","PORT": "3306",},}
# -*- coding: utf-8 -*-# 正式环境RUN_MODE = "PRODUCT"# APP正式环境静态资源目录STATIC_URL = "/static/"# APP静态资源目录url# REMOTE_STATIC_URL = "%sremote/" % STATIC_URL# 正式环境数据库设置DATABASES = {"default": {"ENGINE": "django.db.backends.mysql","NAME": "dbname", # noqa"USER": "root","PASSWORD": "root","HOST": "127.0.0.1","PORT": "3306",},}
# -*- coding: utf-8 -*-# 预发布环境RUN_MODE = "STAGING"# APP预发布环境静态资源目录STATIC_URL = "/static/"# APP静态资源目录url# REMOTE_STATIC_URL = "%sremote/" % STATIC_URLDEBUG = True# 预发布环境数据库设置DATABASES = {"default": {"ENGINE": "django.db.backends.mysql","NAME": "dbname", # noqa"USER": "root","PASSWORD": "root","HOST": "127.0.0.1","PORT": "3306",},}
windows下,在“系统设置”——“环境变量”——“用户变量”下,点击“新建” 添加进去
import os# V3判断环境的环境变量为BKPAAS_ENVIRONMENTif "BKPAAS_ENVIRONMENT" in os.environ:ENVIRONMENT = os.getenv("BKPAAS_ENVIRONMENT", "dev")# V2判断环境的环境变量为BK_ENVelse:PAAS_V2_ENVIRONMENT = os.environ.get("BK_ENV", "development")ENVIRONMENT = {"development": "dev","testing": "stag","production": "prod",}.get(PAAS_V2_ENVIRONMENT)DJANGO_CONF_MODULE = "config.{env}".format(env=ENVIRONMENT)try:_module = __import__(DJANGO_CONF_MODULE, globals(), locals(), ["*"])except ImportError as e:raise ImportError("Could not import config "%s" (Is it on sys.path?): %s"% (DJANGO_CONF_MODULE, e))for _setting in dir(_module):if _setting == _setting.upper():locals()[_setting] = getattr(_module, _setting)
os.environ.get("xx")解释:os.environ.get("xx") # os.environ是字典。get获得环境变量xx的值os.environ.get("BK_ENV", "development") # 找BK_ENV,如果没有就输出development__import__解释:__import__("os",globals(),locals(),["*"]) #等价于from os import *__import__("os",globals(),locals(),["path","pip"]) #等价于from os import path, pipfor解释:for循环全部方法如果全部字符都是大写局部变量locals()的[_setting] = 获取_module的属性 _setting 值