接着昨天的看 源碼閱讀一python
上次看的django-admin 命令下只能執行help 跟一個version。 此次看看check 命令執行流程django
要運行要先配置 一個setting。 你能夠用已有項目裏面的配置文件app
我直接複製一個配置文件到 django-admin.py 的這個文件下面,而且刪掉一些東西ide
""" Django settings for Attendance_system project. Generated by 'django-admin startproject' using Django 1.9.1. For more information on this file, see https://docs.djangoproject.com/en/1.9/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.9/ref/settings/ """ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '3$j)!r#3v_$s96277jyd)hv!2irwzgvkyr8g6c0g6b#cuwt@0='
只剩一個 SECRET_KEY 配置。fetch
而且在運行文件加上一行 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")ui
結構變成這樣了this
先運行一下結果是:System check identified no issues (0 silenced).spa
搞個斷點調試看代碼。
.net
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. """ try: subcommand = self.argv[1] # 若是沒有傳參數就默認輸出help except IndexError: subcommand = 'help' # Display help if no arguments were given. # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are 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) except CommandError: pass # Ignore any option errors at this point. 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: # 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 # In all other cases, django.setup() is required to succeed. 注意這個註釋。 else: django.setup() # check 命令要求這個必需要運行成功。主要作檢查 加載 判斷模塊是否重名等等。 # 有興趣能夠跳進去看看 self.autocomplete() if subcommand == 'help': if '--commands' in args: sys.stdout.write(self.main_help_text(commands_only=True) + '\n') elif len(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. elif subcommand == 'version' or self.argv[1:] == ['--version']: sys.stdout.write(django.get_version() + '\n') elif self.argv[1:] in (['--help'], ['-h']): sys.stdout.write(self.main_help_text() + '\n') else: self.fetch_command(subcommand).run_from_argv(self.argv) # 程序運行進入這裏 # 調用management->commands->check.py 這個文件。 而後就進行了各類check。