废话不多,直接开始了。
既然要看启动,那就要找到程序的入口,django的程序入口在manage.py中:
if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auth.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
这里的
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auth.settings")
是对环境进行添加(占时这么理解了)
那么关键来了
execute_from_command_line(sys.argv)
这是从
django.core.management
中引入的。
def execute_from_command_line(argv=None): """ A simple method that runs a ManagementUtility. """ utility = ManagementUtility(argv) utility.execute()
这里是execute_from_command_lin进行了定义,首先通过参数来获得一个ManageMentUtility对象,这是个命令集合,用来执行命令的管理器。之后通过调用execute函数来执行相应的命令操作,这也是整个启动的核心函数了。
def execute(self): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. parser = LaxOptionParser(usage="%prog subcommand [options] [args]", version=get_version(), option_list=BaseCommand.option_list) try: options, args = parser.parse_args(self.argv) handle_default_options(options) except: # Needed because parser.parse_args can raise SystemExit pass # Ignore any option errors at this point. try: subcommand = self.argv[1] except IndexError: subcommand = 'help' # Display help if no arguments were given. no_settings_commands = [ 'help', 'version', '--help', '--version', '-h', 'compilemessages', 'makemessages', 'startapp', 'startproject', ] try: settings.INSTALLED_APPS except ImproperlyConfigured as exc: self.settings_exception = exc # A handful of built-in management commands work without settings. # Load the default settings -- where INSTALLED_APPS is empty. if subcommand in no_settings_commands: settings.configure() if settings.configured: django.setup() self.autocomplete() if subcommand == 'help': if len(args) <= 2: parser.print_lax_help() sys.stdout.write(self.main_help_text() + '\n') elif args[2] == '--commands': sys.stdout.write(self.main_help_text(commands_only=True) + '\n') else: self.fetch_command(args[2]).print_help(self.prog_name, args[2]) elif subcommand == 'version': sys.stdout.write(parser.get_version() + '\n') # Special-cases: We want 'django-admin.py --version' and # 'django-admin.py --help' to work, for backwards compatibility. elif self.argv[1:] == ['--version']: # LaxOptionParser already takes care of printing the version. pass elif self.argv[1:] in (['--help'], ['-h']): parser.print_lax_help() sys.stdout.write(self.main_help_text() + '\n') else: self.fetch_command(subcommand).run_from_argv(self.argv)
这段函数首先通过分解获得命令参数:
parser = LaxOptionParser(usage="%prog subcommand [options] [args]", version=get_version(), option_list=BaseCommand.option_list) try: options, args = parser.parse_args(self.argv) handle_default_options(options)
对命令的有效性进行分析:
try: subcommand = self.argv[1] except IndexError: subcommand = 'help' # Display help if no arguments were given. no_settings_commands = [ 'help', 'version', '--help', '--version', '-h', 'compilemessages', 'makemessages', 'startapp', 'startproject', ]
加载已经设置的APP模块:
try: settings.INSTALLED_APPS except ImproperlyConfigured as exc: self.settings_exception = exc # A handful of built-in management commands work without settings. # Load the default settings -- where INSTALLED_APPS is empty. if subcommand in no_settings_commands: settings.configure() if settings.configured: django.setup()
在django.setup()中首先会对apps是否加载进行检测,如果没有加载那么会调用load_command_class来import_module来加载这个包
最后
self.fetch_command(subcommand).run_from_argv(self.argv)
run_from_argv函数是来自于base.py主要是同个这个函数来执行相应的命令来启动django