zoukankan      html  css  js  c++  java
  • django从runserver到启动成功

    仿照django自带的admin实现自己的admin组件
    https://docs.djangoproject.com/zh-hans/2.2/ref/contrib/admin/

    django从runserver到启动成功

    运行python manage.py runserver启动
    打开manage.py文件可以发现该文件代码非常简单,主要干了两件事情:

    1. settings.py文件的路径存入系统环境变量os.environos.environ是一个继承自抽象类_collections_abc.MutableMappingos._Environ类的对象。
    2. 将命令行参数sys.argv传给execute_from_command_line执行。execute_from_command_line使用argv创建一个ManagementUtility对象,
    def main():
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Django_Admin.settings')
        try:
            from django.core.management import execute_from_command_line
        except ImportError as exc:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            ) from exc
        execute_from_command_line(sys.argv)
    
    
    if __name__ == '__main__':
        main()
    

    execute_from_command_line

    接着进入execute_from_command_line,execute_from_command_line使用argv创建一个ManagementUtility对象,然后调用其execute方法。

    # django/core/management/__init__.py
    
    def execute_from_command_line(argv=None):
        """Run a ManagementUtility."""
        utility = ManagementUtility(argv)
        utility.execute()
    

    在这时,我们发现由于当前文件import了其他依赖而初试化了apps对象和 settings 对象.

    由于Import django.apps.apps,apps对象会被初始化,这个对象主要负责存储已安装的django app的配置(AppConfig:包括app路径,名称,模块,app_label,显示名称等)以及每个app下的各个model.

    # django/core/management/__init__.py
    
    from django.apps import apps
    from django.conf import settings
    
    # django/apps/registry.py
    
    apps = Apps(installed_apps=None)
    

    由于importdjango.conf.settings而初始化LazySettings类,它继承自LazyObject类,LazyObject通过内嵌函数代理了__bytes__,__str__,__len__,__contains__,__iter__,__delitem__,__setitem__,__getitem__,__hash__,__ne__,__eq__,__class__,__dir__,__bool__方法,使这些方法在被调用之前检查setting是否被加载,如果没有的话,调用self._setup()安装,然后再执行被调用的方法.也就是说所有继承LazyObject的类,在调用上面方法时,会先调用_setup方法,只需要将初始化setting的代码放到_setup里,就可以实现懒加载setting.

    # django/conf/__init__.py
    settings = LazySettings()
    
    # django/utils/functional.py
    def new_method_proxy(func):
        def inner(self, *args):
            if self._wrapped is empty:
                self._setup()
            return func(self._wrapped, *args)
        return inner
    
    # django/utils/functional.py
    class LazyObject:
        ...
    
        # Need to pretend to be the wrapped class, for the sake of objects that
        # care about this (especially in equality tests)
        __class__ = property(new_method_proxy(operator.attrgetter("__class__")))
        __eq__ = new_method_proxy(operator.eq)
        __lt__ = new_method_proxy(operator.lt)
        __gt__ = new_method_proxy(operator.gt)
        __ne__ = new_method_proxy(operator.ne)
        __hash__ = new_method_proxy(hash)
    
        # List/Tuple/Dictionary methods support
        __getitem__ = new_method_proxy(operator.getitem)
        __setitem__ = new_method_proxy(operator.setitem)
        __delitem__ = new_method_proxy(operator.delitem)
        __iter__ = new_method_proxy(iter)
        __len__ = new_method_proxy(len)
        __contains__ = new_method_proxy(operator.contains)
        
        ...
    

    execute

    继续看ManagementUtility,它的execute方法主要完成了,命令行参数解析运行command

    • 命令行参数解析。首先生成一个继承自ArgumentParserCommandParser类,然后通过CommandParser类对参数进行解析。我们在编写django command时继承的BaseCommand就是通过CommandParser来解析命令行参数的。
    • 运行command。解析出subcommand,分别根据subcommand通过fetch_command函数得到各种command对象,而command对象是通过获取djangocoremanagementcommands目录和每个appmanagementcommands下所有py文件中的Command类得到。调用django.setup()logging进行配置并调用apps.populate(settings.INSTALLED_APPS)配置安装app。然后由于Command类都继承自BaseCommand,只需要调用run_from_argv方法即可对子命令行参数执行。
    # django/core/management/__init__.py
    
        def execute(self):
            """
            Given the command-line arguments, figure out which subcommand is being
            run, create a parser appropriate to that command, and run it.
            """
            try:
                subcommand = self.argv[1]
            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(usage='%(prog)s subcommand [options] [args]', add_help=False, allow_abbrev=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.
    
            try:
                settings.INSTALLED_APPS
            except ImproperlyConfigured as exc:
                self.settings_exception = exc
            except ImportError as exc:
                self.settings_exception = exc
    
            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
    
                        # 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()
    
            self.autocomplete()
    
            if subcommand == 'help':
                if '--commands' in args:
                    sys.stdout.write(self.main_help_text(commands_only=True) + '
    ')
                elif not options.args:
                    sys.stdout.write(self.main_help_text() + '
    ')
                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() + '
    ')
            elif self.argv[1:] in (['--help'], ['-h']):
                sys.stdout.write(self.main_help_text() + '
    ')
            else:
                self.fetch_command(subcommand).run_from_argv(self.argv)
    

    autoreload.check_errors(django.setup)()其实也是调用django.setup方法

    def setup(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.
        """
        from django.apps import apps
        from django.conf import settings
        from django.urls import set_script_prefix
        from django.utils.log import configure_logging
    
        configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
        if set_prefix:
            set_script_prefix(
                '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
            )
        apps.populate(settings.INSTALLED_APPS)
    

    apps.populate(settings.INSTALLED_APPS)

    apps.populate(settings.INSTALLED_APPS) 将会根据settings.INSTALLED_APPS中的配置查找app,之后分别调用每个app_configready方法。由于apps.populate是加了锁防止重复执行的,所以每个appapp_config.ready只会初始化一次,我们如果有需要在django启动时执行一次的操作,可以将其放入app_config.ready中。

    在调用app_config.ready之前,会先将所有的models加载进来。其通过MODELS_MODULE_NAME="models"; import_module(app_name,MODELS_MODULE_NAME)的方式导入models

        def populate(self, installed_apps=None):
            """
            Load application configurations and models.
    
            Import each application module and then each model module.
    
            It is thread-safe and idempotent, but not reentrant.
            """
            if self.ready:
                return
    
            # populate() might be called by two threads in parallel on servers
            # that create threads before initializing the WSGI callable.
            with self._lock:
                if self.ready:
                    return
    
                # An RLock prevents other threads from entering this section. The
                # compare and set operation below is atomic.
                if self.loading:
                    # Prevent reentrant calls to avoid running AppConfig.ready()
                    # methods twice.
                    raise RuntimeError("populate() isn't reentrant")
                self.loading = True
    
                # Phase 1: initialize app configs and import app modules.
                for entry in installed_apps:
                    if isinstance(entry, AppConfig):
                        app_config = entry
                    else:
                        app_config = AppConfig.create(entry)
                    if app_config.label in self.app_configs:
                        raise ImproperlyConfigured(
                            "Application labels aren't unique, "
                            "duplicates: %s" % app_config.label)
    
                    self.app_configs[app_config.label] = app_config
                    app_config.apps = self
    
                # Check for duplicate app names.
                counts = Counter(
                    app_config.name for app_config in self.app_configs.values())
                duplicates = [
                    name for name, count in counts.most_common() if count > 1]
                if duplicates:
                    raise ImproperlyConfigured(
                        "Application names aren't unique, "
                        "duplicates: %s" % ", ".join(duplicates))
    
                self.apps_ready = True
    
                # Phase 2: import models modules.
                for app_config in self.app_configs.values():
                    app_config.import_models()
    
                self.clear_cache()
    
                self.models_ready = True
    
                # Phase 3: run ready() methods of app configs.
                for app_config in self.get_app_configs():
                    app_config.ready()
    
                self.ready = True
                self.ready_event.set()
    
        def import_models(self):
            # Dictionary of models for this app, primarily maintained in the
            # 'all_models' attribute of the Apps this AppConfig is attached to.
            self.models = self.apps.all_models[self.label]
    
            if module_has_submodule(self.module, MODELS_MODULE_NAME):
                models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
                self.models_module = import_module(models_module_name)
    

    AdminConfig

    看一下adminAdminConfig

    
    class SimpleAdminConfig(AppConfig):
        """Simple AppConfig which does not do automatic discovery."""
    
        default_site = 'django.contrib.admin.sites.AdminSite'
        name = 'django.contrib.admin'
        verbose_name = _("Administration")
    
        def ready(self):
            checks.register(check_dependencies, checks.Tags.admin)
            checks.register(check_admin_app, checks.Tags.admin)
    
    
    class AdminConfig(SimpleAdminConfig):
        """The default AppConfig for admin which does autodiscovery."""
    
        def ready(self):
            super().ready()
            self.module.autodiscover()
    

    自动扫描每个app下的admin包, 将里面的东西注册到site = DefaultAdminSite()
    site是一个单例对象

    def autodiscover():
        autodiscover_modules('admin', register_to=site)
    

    autodiscover_modules函数自动加载每个app下的admin.py文件

    def autodiscover_modules(*args, **kwargs):
        """
        Auto-discover INSTALLED_APPS modules and fail silently when
        not present. This forces an import on them to register any admin bits they
        may want.
    
        You may provide a register_to keyword parameter as a way to access a
        registry. This register_to object must have a _registry instance variable
        to access it.
        """
        from django.apps import apps
    
        register_to = kwargs.get('register_to')
        for app_config in apps.get_app_configs():
            for module_to_search in args:
                # Attempt to import the app's module.
                try:
                    if register_to:
                        before_import_registry = copy.copy(register_to._registry)
    
                    import_module('%s.%s' % (app_config.name, module_to_search))
                except Exception:
                    # Reset the registry to the state before the last import
                    # as this import will have to reoccur on the next request and
                    # this could raise NotRegistered and AlreadyRegistered
                    # exceptions (see #8245).
                    if register_to:
                        register_to._registry = before_import_registry
    
                    # Decide whether to bubble up this error. If the app just
                    # doesn't have the module in question, we can ignore the error
                    # attempting to import it, otherwise we want it to bubble up.
                    if module_has_submodule(app_config.module, module_to_search):
                        raise
    

    admin.site.register()

        def register(self, model_or_iterable, admin_class=None, **options):
            """
            Register the given model(s) with the given admin class.
    
            The model(s) should be Model classes, not instances.
    
            If an admin class isn't given, use ModelAdmin (the default admin
            options). If keyword arguments are given -- e.g., list_display --
            apply them as options to the admin class.
    
            If a model is already registered, raise AlreadyRegistered.
    
            If a model is abstract, raise ImproperlyConfigured.
            """
            admin_class = admin_class or ModelAdmin
            if isinstance(model_or_iterable, ModelBase):
                model_or_iterable = [model_or_iterable]
            for model in model_or_iterable:
                if model._meta.abstract:
                    raise ImproperlyConfigured(
                        'The model %s is abstract, so it cannot be registered with admin.' % model.__name__
                    )
    
                if model in self._registry:
                    raise AlreadyRegistered('The model %s is already registered' % model.__name__)
    
                # Ignore the registration if the model has been
                # swapped out.
                if not model._meta.swapped:
                    # If we got **options then dynamically construct a subclass of
                    # admin_class with those **options.
                    if options:
                        # For reasons I don't quite understand, without a __module__
                        # the created class appears to "live" in the wrong place,
                        # which causes issues later on.
                        options['__module__'] = __name__
                        admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
    
                    # Instantiate the admin class to save in the registry
                    self._registry[model] = admin_class(model, self)
    
    print(admin.site._registry)
    
    {<class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x0000019BA62B9BE0>, 
    <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x0000019BA62E9BA8>, 
    <class 'book.models.Book'>: <django.contrib.admin.options.ModelAdmin object at 0x0000019BA62E9DD8>, 
    <class 'book.models.Author'>: <django.contrib.admin.options.ModelAdmin object at 0x0000019BA62FDC50>}
    

    回到execute函数中

    self.fetch_command(subcommand).run_from_argv(self.argv)为重点,是用户输入的命令的真实入口

        def fetch_command(self, subcommand):
            """
            Try to fetch the given subcommand, printing a message with the
            appropriate command called from the command line (usually
            "django-admin" or "manage.py") if it can't be found.
            """
            # Get commands outside of try block to prevent swallowing exceptions
            commands = get_commands()
            try:
                app_name = commands[subcommand]
            except KeyError:
                if os.environ.get('DJANGO_SETTINGS_MODULE'):
                    # If `subcommand` is missing due to misconfigured settings, the
                    # following line will retrigger an ImproperlyConfigured exception
                    # (get_commands() swallows the original one) so the user is
                    # informed about it.
                    settings.INSTALLED_APPS
                else:
                    sys.stderr.write("No Django settings specified.
    ")
                possible_matches = get_close_matches(subcommand, commands)
                sys.stderr.write('Unknown command: %r' % subcommand)
                if possible_matches:
                    sys.stderr.write('. Did you mean %s?' % possible_matches[0])
                sys.stderr.write("
    Type '%s help' for usage.
    " % self.prog_name)
                sys.exit(1)
            if isinstance(app_name, BaseCommand):
                # If the command is already loaded, use it directly.
                klass = app_name
            else:
                klass = load_command_class(app_name, subcommand)
            return klass
    
    def load_command_class(app_name, name):
        """
        Given a command name and an application name, return the Command
        class instance. Allow all errors raised by the import process
        (ImportError, AttributeError) to propagate.
        """
        module = import_module('%s.management.commands.%s' % (app_name, name))
        return module.Command()
    

    其中fetch_command根据subcommand返回对应的命令处理文件,load_command_class函数返回module.Command(),在本例中就是self.fetch_command(subcommand)就是runserver.Command()

    命令处理文件放在django/core/management/commands下面
    打开django/core/management/commands/runserver.py文件
    Command类继承了BaseCommand类, 查看BaseCommandrun_from_argv方法

        def run_from_argv(self, argv):
            """
            Set up any environment changes requested (e.g., Python path
            and Django settings), then run this command. If the
            command raises a ``CommandError``, intercept it and print it sensibly
            to stderr. If the ``--traceback`` option is present or the raised
            ``Exception`` is not ``CommandError``, raise it.
            """
            self._called_from_command_line = True
            parser = self.create_parser(argv[0], argv[1])
    
            options = parser.parse_args(argv[2:])
            cmd_options = vars(options)
            # Move positional args out of options to mimic legacy optparse
            args = cmd_options.pop('args', ())
            handle_default_options(options)
            try:
                self.execute(*args, **cmd_options)
            except Exception as e:
                if options.traceback or not isinstance(e, CommandError):
                    raise
    
                # SystemCheckError takes care of its own formatting.
                if isinstance(e, SystemCheckError):
                    self.stderr.write(str(e), lambda x: x)
                else:
                    self.stderr.write('%s: %s' % (e.__class__.__name__, e))
                sys.exit(1)
            finally:
                try:
                    connections.close_all()
                except ImproperlyConfigured:
                    # Ignore if connections aren't setup at this point (e.g. no
                    # configured settings).
                    pass
    

    execute函数返回的是handle函数

        def execute(self, *args, **options):
            """
            Try to execute this command, performing system checks if needed (as
            controlled by the ``requires_system_checks`` attribute, except if
            force-skipped).
            """
            if options['force_color'] and options['no_color']:
                raise CommandError("The --no-color and --force-color options can't be used together.")
            if options['force_color']:
                self.style = color_style(force_color=True)
            elif options['no_color']:
                self.style = no_style()
                self.stderr.style_func = None
            if options.get('stdout'):
                self.stdout = OutputWrapper(options['stdout'])
            if options.get('stderr'):
                self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func)
    
            if self.requires_system_checks and not options.get('skip_checks'):
                self.check()
            if self.requires_migrations_checks:
                self.check_migrations()
            output = self.handle(*args, **options)
            if output:
                if self.output_transaction:
                    connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
                    output = '%s
    %s
    %s' % (
                        self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()),
                        output,
                        self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()),
                    )
                self.stdout.write(output)
            return output
    

    查看Command类的handle函数, Command重写了父类的handle方法

        def handle(self, *args, **options):
            if not settings.DEBUG and not settings.ALLOWED_HOSTS:
                raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
    
            self.use_ipv6 = options['use_ipv6']
            if self.use_ipv6 and not socket.has_ipv6:
                raise CommandError('Your Python does not support IPv6.')
            self._raw_ipv6 = False
            if not options['addrport']:
                self.addr = ''
                self.port = self.default_port
            else:
                m = re.match(naiveip_re, options['addrport'])
                if m is None:
                    raise CommandError('"%s" is not a valid port number '
                                       'or address:port pair.' % options['addrport'])
                self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
                if not self.port.isdigit():
                    raise CommandError("%r is not a valid port number." % self.port)
                if self.addr:
                    if _ipv6:
                        self.addr = self.addr[1:-1]
                        self.use_ipv6 = True
                        self._raw_ipv6 = True
                    elif self.use_ipv6 and not _fqdn:
                        raise CommandError('"%s" is not a valid IPv6 address.' % self.addr)
            if not self.addr:
                self.addr = self.default_addr_ipv6 if self.use_ipv6 else self.default_addr
                self._raw_ipv6 = self.use_ipv6
            self.run(**options)
    
        def run(self, **options):
            """Run the server, using the autoreloader if needed."""
            use_reloader = options['use_reloader']
    
            if use_reloader:
                autoreload.run_with_reloader(self.inner_run, **options)
            else:
                self.inner_run(None, **options)
    
        def inner_run(self, *args, **options):
            # If an exception was silenced in ManagementUtility.execute in order
            # to be raised in the child process, raise it now.
            autoreload.raise_last_exception()
    
            threading = options['use_threading']
            # 'shutdown_message' is a stealth option.
            shutdown_message = options.get('shutdown_message', '')
            quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
    
            self.stdout.write("Performing system checks...
    
    ")
            self.check(display_num_errors=True)
            # Need to check migrations here, so can't use the
            # requires_migrations_check attribute.
            self.check_migrations()
            now = datetime.now().strftime('%B %d, %Y - %X')
            self.stdout.write(now)
            self.stdout.write((
                "Django version %(version)s, using settings %(settings)r
    "
                "Starting development server at %(protocol)s://%(addr)s:%(port)s/
    "
                "Quit the server with %(quit_command)s.
    "
            ) % {
                "version": self.get_version(),
                "settings": settings.SETTINGS_MODULE,
                "protocol": self.protocol,
                "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                "port": self.port,
                "quit_command": quit_command,
            })
    
            try:
                handler = self.get_handler(*args, **options)
                run(self.addr, int(self.port), handler,
                    ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
            except socket.error as e:
                # Use helpful error messages instead of ugly tracebacks.
                ERRORS = {
                    errno.EACCES: "You don't have permission to access that port.",
                    errno.EADDRINUSE: "That port is already in use.",
                    errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
                }
                try:
                    error_text = ERRORS[e.errno]
                except KeyError:
                    error_text = e
                self.stderr.write("Error: %s" % error_text)
                # Need to use an OS exit because sys.exit doesn't work in a thread
                os._exit(1)
            except KeyboardInterrupt:
                if shutdown_message:
                    self.stdout.write(shutdown_message)
                sys.exit(0)
    

    最终通过WSGIServer类启动线程来处理http请求

    def run(addr, port, wsgi_handler, ipv6=False, threading=False, server_cls=WSGIServer):
        server_address = (addr, port)
        if threading:
            httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {})
        else:
            httpd_cls = server_cls
        httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
        if threading:
            # ThreadingMixIn.daemon_threads indicates how threads will behave on an
            # abrupt shutdown; like quitting the server by the user or restarting
            # by the auto-reloader. True means the server will not wait for thread
            # termination before it quits. This will make auto-reloader faster
            # and will prevent the need to kill the server manually if a thread
            # isn't terminating correctly.
            httpd.daemon_threads = True
        httpd.set_app(wsgi_handler)
        httpd.serve_forever()
    

    启动流程

    runserver方式以command的方式启动wsgi. 先初始化appssettings对象;接着获得所有command对象;懒加载调用 setting._setup()方法安装配置;接着解析得到子命令,调用django.setup()配置logging和初始化app_config,导入models;根据子命令获取command对象并将参数传递给其并执行,其他参数根据managementcommands中的command文件的定义进行解析,然后调用handle方法进行执行;然后调用run方法启动wsgi.

    参考:
    https://www.cnblogs.com/arrow-kejin/p/10359468.html

  • 相关阅读:
    Sublime Text 3专题
    00.PHP学习建议
    php面试题及答案收藏(转)
    (转)如何快速掌握一门技术
    最全的MySQL基础【燕十八传世】
    UEditor编辑器并不难
    学习一门语言难在什么地方?
    PHP中关于位运算符 与 或 异或 取反
    WebP是什么图片格式?你了解PNG、JPG、WebP和GIF的区别吗?
    Mysql 外键设置
  • 原文地址:https://www.cnblogs.com/gaoyongjian/p/10871265.html
Copyright © 2011-2022 走看看