defmain(): # 将根目录下的setting.py文件利用基于字符串驱动的反射机制,添加到os.environ的字典中,以DJANGO_SETTINGS_MODULE作为键值。 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mblog.settings') try: # 获取控制台启动命令,会检查是否导入所需要的模块 from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv)
classLazySettings(LazyObject): """ A lazy proxy for either global Django settings or a custom settings object. The user can manually configure settings prior to using them. Otherwise, Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE. """ # 启动项 def_setup(self, name=None): """ Load the settings module pointed to by the environment variable. This is used the first time settings are needed, if the user hasn't configured settings manually. """ # 利用反射机制字典中DJANGO_SETTINGS_MODULE键对应的根目录下的settings.py settings_module = os.environ.get(ENVIRONMENT_VARIABLE) ifnot settings_module: desc = ("setting %s" % name) if name else"settings" raise ImproperlyConfigured( "Requested %s, but settings are not configured. " "You must either define the environment variable %s " "or call settings.configure() before accessing settings." % (desc, ENVIRONMENT_VARIABLE)) # 配置settings.py文件内容,进行文件的覆盖和初始化 self._wrapped = Settings(settings_module)
def__repr__(self): # Hardcode the class name as otherwise it yields 'Settings'. if self._wrapped is empty: return'<LazySettings [Unevaluated]>' return'<LazySettings "%(settings_module)s">' % { 'settings_module': self._wrapped.SETTINGS_MODULE, } ...
classSettings: def__init__(self, settings_module): # update this dict from global settings (but only for ALL_CAPS settings) # dir()获取当前模块的属性,方法列表 for setting indir(global_settings): if setting.isupper(): # 通过反射机制获取global_settings的配置内让能够 setattr(self, setting, getattr(global_settings, setting))
# store the settings module in case someone later cares # 将根目录下的settings文件赋给self.SETTINGS_MODULE self.SETTINGS_MODULE = settings_module
# 导入该配置模块 mod = importlib.import_module(self.SETTINGS_MODULE)
if (setting in tuple_settings and notisinstance(setting_value, (list, tuple))): raise ImproperlyConfigured("The %s setting must be a list or a tuple. " % setting) # 利用反射将setting_value添加到self._explicit_settings集合中去 setattr(self, setting, setting_value) self._explicit_settings.add(setting)
ifnot self.SECRET_KEY: # 检查是否存在密钥,每个django项目都有唯一的密钥 raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
if self.is_overridden('DEFAULT_CONTENT_TYPE'): warnings.warn(DEFAULT_CONTENT_TYPE_DEPRECATED_MSG, RemovedInDjango30Warning) if self.is_overridden('FILE_CHARSET'): warnings.warn(FILE_CHARSET_DEPRECATED_MSG, RemovedInDjango31Warning)
ifhasattr(time, 'tzset') and self.TIME_ZONE: # 配置self.TIME_ZONE # When we can, attempt to validate the timezone. If we can't find # this file, no check happens and it's harmless. zoneinfo_root = Path('/usr/share/zoneinfo') zone_info_file = zoneinfo_root.joinpath(*self.TIME_ZONE.split('/')) if zoneinfo_root.exists() andnot zone_info_file.exists(): raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE) # Move the time zone info into os.environ. See ticket #2315 for why # we don't do this unconditionally (breaks Windows). os.environ['TZ'] = self.TIME_ZONE time.tzset()
defis_overridden(self, setting): # 判断是否覆盖了默认的配置文件 return setting in self._explicit_settings