1. Django Startup
1.1. Start command
Execute the start command in the Django Project root directory, as follows:
Python manage.py runserver 8008
1.2, the implementation of manage.py
manage.py Source:
if __name__=="__main__": Os.environ.setdefault ("Django_settings_module","middleware_demo.settings") Try: fromDjango.core.managementImportExecute_from_command_lineexceptImporterror:#The above import may fail for some and other reason. Ensure that the #issue is really, Django is missing to avoid masking other #exceptions on Python 2. Try: ImportDjangoexceptImporterror:RaiseImporterror ("couldn ' t import Django. Is you sure it ' s installed and" "available on your PYTHONPATH environment variable? Did you" "forget to activate a virtual environment?" ) Raiseexecute_from_command_line (SYS.ARGV)
Analytical:
- Set the system environment variables first
- Import the Execute_from_command_line () function from the Django.core.management module to perform commands entered from the terminal, such as "Python manage.py runserver 8008".
- If an import error (Importerror) occurs in the previous step, try importing Django, and if an import error occurs (importerror), throw a importerror error, Description does not import Django properly, letting you confirm that Django is installed and register its installation directory with environment variables.
- Finally execute execute_from_command_line(sys.argv) to execute the command entered from terminal cmd
1.3, the implementation of Execute_from_command_line
1.3.1, source code
def execute_from_command_line (argv=None): "" "Run a managementutility. """ = managementutility (argv) utility.execute ()
1.3.2, parsing
- Instantiate class Managementutility
Utility = Managementutility (argv)
Instantiate the parameter argv incoming class managementutility and assign the Self.prog_name to ' manage.py '.
- Execute () method that executes the instantiated object
Utility.execute ()
1.4, the implementation of Utility.execute ()
1.4.1, source code
defExecute (self):"""Given The command-line arguments, figure out which subcommand is being run, create a parser appropriate To the command, and run it. """ Try: subcommand= Self.argv[1] exceptIndexerror:subcommand=' Help' #Display Help If no arguments were given. #preprocess options to extract--settings and--pythonpath. #these options could affect the commands that is available, so they #must be processed early.Parser = Commandparser (None, usage="% (Prog) s subcommand [options] [args]", add_help=False) parser.add_argument ('--settings') parser.add_argument ('--pythonpath') parser.add_argument ('args', nargs='*')#Catch-all Try: Options, args= Parser.parse_known_args (self.argv[2:]) Handle_default_options (Options)exceptCommandError:Pass #Ignore any option errors at the this point. Try: Settings. Installed_appsexceptimproperlyconfigured as Exc:self.settings_exception=excifsettings.configured:#Start the auto-reloading dev server even if the code is broken. #The hardcoded condition is a code smell and we can ' t rely on a #flag on the command class because we haven ' t located it yet. ifsubcommand = ='Runserver' and '--noreload' not inchSELF.ARGV:Try: Autoreload.check_errors (Django.setup) ()exceptException:#The exception'll 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_arginch_args:self.argv.remove (_arg)#in all other cases, django.setup () are required to succeed. Else: Django.setup () self.autocomplete ()ifsubcommand = =' Help': if '--commands' inchArgs:sys.stdout.write (Self.main_help_text (commands_only=true) +'\ n') elifLen (Options.args) < 1: Sys.stdout.write (Self.main_help_text ()+'\ n') Else: Self.fetch_command (Options.args[0]). Print_help (Self.prog_name, options.args[0])#Special-cases:we want ' django-admin--version ' and #' Django-admin--help ' to work, for backwards compatibility. elifsubcommand = ='version' orSelf.argv[1:] = = ['--version']: Sys.stdout.write (django.get_version ()+'\ n') elifSelf.argv[1:]inch(['--help'], ['- H']): Sys.stdout.write (Self.main_help_text ()+'\ n') Else: Self.fetch_command (subcommand). RUN_FROM_ARGV (SELF.ARGV)
1.4.2, parsing
Django Source Parsing (1): Startup program