django/conf/__init__.py/class_Settings

來源:互聯網
上載者:User

標籤:default   cal   only   異常   condition   elf   this   oba   prope   

摘自如題檔案內容,如有侵權,請聯絡 QQ 929054468 刪除
class Settings:
def __init__(self, settings_module):
# update this dict from global settings (but only for ALL_CAPS settings)
for setting in dir(global_settings):
if setting.isupper():
setattr(self, setting, getattr(global_settings, setting))

將本地模組 global_settings 中的屬性值匯入到執行個體對象中

# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module

mod = importlib.import_module(self.SETTINGS_MODULE)

利用 importlib模組匯入新增的模組 SETTINGS_MODULE
        tuple_settings = (
"INSTALLED_APPS",
"TEMPLATE_DIRS",
"LOCALE_PATHS",
)
self._explicit_settings = set()
for setting in dir(mod):
if setting.isupper():
setting_value = getattr(mod, setting)

if (setting in tuple_settings and
not isinstance(setting_value, (list, tuple))):
raise ImproperlyConfigured("The %s setting must be a list or a tuple. " % setting)
setattr(self, setting, setting_value)
self._explicit_settings.add(setting)

將新增的模組屬性值匯入到執行個體對象中

if not self.SECRET_KEY:
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

檢測新匯入的屬性值中有沒有 SECRET_KEY 屬性值,且不為空白
        if self.is_overridden(‘DEFAULT_CONTENT_TYPE‘):
warnings.warn(‘The DEFAULT_CONTENT_TYPE setting is deprecated.‘, RemovedInDjango30Warning)

檢查屬性名稱 DEFAULT_CONTENT_TYPE 是否在新匯入的模組屬性中,否則進行warn提醒
        if hasattr(time, ‘tzset‘) and 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 = ‘/usr/share/zoneinfo‘
if (os.path.exists(zoneinfo_root) and not
os.path.exists(os.path.join(zoneinfo_root, *(self.TIME_ZONE.split(‘/‘))))):
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()

設定時區,首先會檢查 time 模組是否有 tzset 方法,和對象匯入的模組是否有TIME_ZONE屬性,根據TIME_ZONE在linux伺服器的 /usr/share/zoneinfo 尋找對應的時區,如果對應的時區檔案不存在,則拋出異常,
然後重新設定時區

def is_overridden(self, setting):
return setting in self._explicit_settings

def __repr__(self):
return ‘<%(cls)s "%(settings_module)s">‘ % {
‘cls‘: self.__class__.__name__,
‘settings_module‘: self.SETTINGS_MODULE,
}

django/conf/__init__.py/class_Settings

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.