django 源码阅读二 命令check

接着昨天的看 源码阅读一

上次看的django-admin 命令下只能执行help 跟一个version。 这次看看check 命令执行流程

要运行要先配置 一个setting。 你可以用已有项目里面的配置文件

我直接复制一个配置文件到 django-admin.py 的这个文件下面,并且删掉一些东西

"""
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  配置。

并且在运行文件加上一行  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

结构变成这样了

django 源码阅读二 命令check_第1张图片

 先运行一下结果是:System check identified no issues (0 silenced).

搞个断点调试看代码。

  

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。



你可能感兴趣的:(django 源码阅读二 命令check)