# django\core\management\__init__.pydefexecute(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'notin 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 中
defsetup(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 isNoneelse settings.FORCE_SCRIPT_NAME
)
apps.populate(settings.INSTALLED_APPS)
defpopulate(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:ifisinstance(entry, AppConfig):
app_config = entry
else:
app_config = AppConfig.create(entry)...
self.app_configs[app_config.label]= app_config
app_config.apps = self
...
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)