Django 소스 읽기 (2) INSTALLEDAPPS 로드 프로세스
15976 단어 Django 소스 읽기
# django\core\management\__init__.py
def execute(self):
...
if settings.configured:
# Start the auto-reloading dev server even if the code is broken.
# The hardcoded condition is a code smell but we can't rely on a
# flag on the command class because we haven't located it yet.
if subcommand == 'runserver' and '--noreload' not in self.argv:
try:
autoreload.check_errors(django.setup)()
except Exception:
# The exception will be raised later in the child process
# started by the autoreloader. Pretend it didn't happen by
# loading an empty list of applications.
apps.all_models = defaultdict(OrderedDict)
apps.app_configs = OrderedDict()
apps.apps_ready = apps.models_ready = apps.ready = True
# Remove options not compatible with the built-in runserver
# (e.g. options for the contrib.staticfiles' runserver).
# Changes here require manually testing as described in
# #27522.
_parser = self.fetch_command('runserver').create_parser('django', 'runserver')
_options, _args = _parser.parse_known_args(self.argv[2:])
for _arg in _args:
self.argv.remove(_arg)
# In all other cases, django.setup() is required to succeed.
else:
django.setup()
...
django.setup() 이니시에이터
이 코드는django/init.py중
def setup(set_prefix=True):
"""
Configure the settings (this happens as a side effect of accessing the
first setting), configure logging and populate the app registry.
Set the thread-local urlresolvers script prefix if `set_prefix` is True.
"""
...
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
if set_prefix:
set_script_prefix(
'/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
)
apps.populate(settings.INSTALLED_APPS)
모형의 로드
def populate(self, installed_apps=None):
# populate() might be called by two threads in parallel on servers
# that create threads before initializing the WSGI callable.
with self._lock:
...
for entry in installed_apps:
if isinstance(entry, AppConfig):
app_config = entry
else:
app_config = AppConfig.create(entry)
...
self.app_configs[app_config.label] = app_config
app_config.apps = self
...
4
4
try:
app_name = cls.name
except AttributeError:
raise ImproperlyConfigured(
"'%s' must supply a name attribute." % entry)
# Ensure app_name points to a valid module.
try:
app_module = import_module(app_name)
4
# Phase 2: import models modules.
for app_config in self.app_configs.values():
# , app
app_config.import_models()
self.clear_cache()
self.models_ready = True
# Phase 3: run ready() methods of app configs.
for app_config in self.get_app_configs():
#
app_config.ready()
self.ready = True
self.ready_event.set()
# self.get_app_configs()
odict_values([<AdminConfig: admin>,
<AuthConfig: auth>,
<ContentTypesConfig: contenttypes>,
<SessionsConfig: sessions>,
<MessagesConfig: messages>,
...
<AppConfig: pure_pagination>])
# import_models self.models_module
<module 'django.contrib.admin.models' from 'C:\\mooc_project\\lib\\site-packages\\django\\contrib\\admin\\models.py'>
<module 'django.contrib.auth.models' from 'C:\\mooc_project\\lib\\site-packages\\django\\contrib\\auth\\models.py'>
<module 'django.contrib.contenttypes.models' from 'C:\\mooc_project\\lib\\site-packages\\django\\contrib\\contenttypes\\models.py'>
<module 'django.contrib.sessions.models' from 'C:\\mooc_project\\lib\\site-packages\\django\\contrib\\sessions\\models.py'>
# ,self.models 。
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django 소스 읽기 (2) INSTALLEDAPPS 로드 프로세스다시 상절에서 처음 시작한 excute 함수로 돌아가서django를 분석해 봅시다.setup () 함수, 그 안에 어떻게 불러오는 설정을 하는지 보십시오 django.setup() 이니시에이터 이 코드는django/...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.