• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共43篇

    python3.X - Web - Django3.2.9

关闭

返回栏目

关闭

返回python3.X - Web - Django3.2.9栏目

10 - 文件详解 - settings.py + 测试、生产、正式环境

作者:

贺及楼

成为作者

更新日期:2021-12-02 22:21:10

Django测试、生产、正式环境

项目/config/init.py

  1. 项目/config/__init__.py

项目/config/dev.py 本地开发环境

  1. # -*- coding: utf-8 -*-
  2. # 本地开发环境
  3. RUN_MODE = "DEVELOP"
  4. # APP本地静态资源目录
  5. STATIC_URL = "/static/"
  6. # APP静态资源目录url
  7. # REMOTE_STATIC_URL = "%sremote/" % STATIC_URL
  8. DEBUG = True
  9. # 本地开发数据库设置
  10. DATABASES = {
  11. "default": {
  12. "ENGINE": "django.db.backends.mysql",
  13. "NAME": "dbname", # noqa
  14. "USER": "root",
  15. "PASSWORD": "root",
  16. "HOST": "127.0.0.1",
  17. "PORT": "3306",
  18. },
  19. }

项目/config/prod.py 正式环境

  1. # -*- coding: utf-8 -*-
  2. # 正式环境
  3. RUN_MODE = "PRODUCT"
  4. # APP正式环境静态资源目录
  5. STATIC_URL = "/static/"
  6. # APP静态资源目录url
  7. # REMOTE_STATIC_URL = "%sremote/" % STATIC_URL
  8. # 正式环境数据库设置
  9. DATABASES = {
  10. "default": {
  11. "ENGINE": "django.db.backends.mysql",
  12. "NAME": "dbname", # noqa
  13. "USER": "root",
  14. "PASSWORD": "root",
  15. "HOST": "127.0.0.1",
  16. "PORT": "3306",
  17. },
  18. }

项目/config/stag.py 预发布环境

  1. # -*- coding: utf-8 -*-
  2. # 预发布环境
  3. RUN_MODE = "STAGING"
  4. # APP预发布环境静态资源目录
  5. STATIC_URL = "/static/"
  6. # APP静态资源目录url
  7. # REMOTE_STATIC_URL = "%sremote/" % STATIC_URL
  8. DEBUG = True
  9. # 预发布环境数据库设置
  10. DATABASES = {
  11. "default": {
  12. "ENGINE": "django.db.backends.mysql",
  13. "NAME": "dbname", # noqa
  14. "USER": "root",
  15. "PASSWORD": "root",
  16. "HOST": "127.0.0.1",
  17. "PORT": "3306",
  18. },
  19. }

电脑的虚拟环境

windows下,在“系统设置”——“环境变量”——“用户变量”下,点击“新建” 添加进去

判断是什么环境 settings.py

  1. import os
  2. # V3判断环境的环境变量为BKPAAS_ENVIRONMENT
  3. if "BKPAAS_ENVIRONMENT" in os.environ:
  4. ENVIRONMENT = os.getenv("BKPAAS_ENVIRONMENT", "dev")
  5. # V2判断环境的环境变量为BK_ENV
  6. else:
  7. PAAS_V2_ENVIRONMENT = os.environ.get("BK_ENV", "development")
  8. ENVIRONMENT = {
  9. "development": "dev",
  10. "testing": "stag",
  11. "production": "prod",
  12. }.get(PAAS_V2_ENVIRONMENT)
  13. DJANGO_CONF_MODULE = "config.{env}".format(env=ENVIRONMENT)
  14. try:
  15. _module = __import__(DJANGO_CONF_MODULE, globals(), locals(), ["*"])
  16. except ImportError as e:
  17. raise ImportError("Could not import config "%s" (Is it on sys.path?): %s"
  18. % (DJANGO_CONF_MODULE, e))
  19. for _setting in dir(_module):
  20. if _setting == _setting.upper():
  21. locals()[_setting] = getattr(_module, _setting)
  1. os.environ.get("xx")解释:
  2. os.environ.get("xx") # os.environ是字典。get获得环境变量xx的值
  3. os.environ.get("BK_ENV", "development") # 找BK_ENV,如果没有就输出development
  4. __import__解释:
  5. __import__("os",globals(),locals(),["*"]) #等价于from os import *
  6. __import__("os",globals(),locals(),["path","pip"]) #等价于from os import path, pip
  7. for解释:
  8. for循环全部方法
  9. 如果全部字符都是大写
  10. 局部变量locals()的[_setting] = 获取_module的属性 _setting

调用