zoukankan      html  css  js  c++  java
  • Django项目启动之前执行流程剖析

    下面,我们只看看主要的步骤:

    1.项目启动,遍历settings下面的INSTALLED_APPS,导入默认配置。

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app01.apps.App01Config',
        'app02.apps.App02Config',
        'niubin.apps.NiubinConfig',
    ]

    2.接下来执行apps.populate(settings.INSTALLED_APPS)函数,该函数主要完成了以下几个步骤:

      step-one:

    # 步骤一:初始化app配置并且导入app模块
        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

      step-two:

     # 步骤二:导入model模块
        for app_config in self.app_configs.values():
            app_config.import_models()

      step-three:

    # 步骤三:执行每个app模块中的ready()方法
        for app_config in self.get_app_configs():
            #app_config:<AuthConfig: auth>
            #app_config:<SessionsConfig: sessions>
            app_config.ready()# 这里要注意,如何apps中没有定义ready()方法,就会执行AppConfig类中的默认ready()方法

    3.如果上述过程中的ready()函数执行了autodiscover()函数,如:autodiscover('nb'),就会完成以下操作:

    def autodiscover_modules(*args, **kwargs):
            '''搜索每个app下面的'admin'''
            # args:'amdmin'
            from django.apps import apps
            register_to = kwargs.get('register_to')
            for app_config in apps.get_app_configs():
                # app_config:<AuthConfig: auth>....
                for module_to_search in args:
                    # module_to_search:'admin'
                    # module_to_search:'niubi' 搜索每个app下的nb模块
                    pass

     這个函数完成的主要功能就是,搜索每个app下面的nb模块。也就是说,如果autodiscover('admin'),就会寻找每个app下面的admin模块,我们都知道,admin模块主要完成model的注册功能,那么只要调用了自动发现函数,那么函数的注册功能就在这一步完成的!

    4.我们顺着admin.site.register()函数继续往下分析,这步操作完成了以下操作:

    site = AdminSite() # 生成了一个全局的site实例

      接着,该实例调用了register函数,我们跟进去看看,這个函数做了什么操作:

        def register(self, model_or_iterable, admin_class=None, **options):
            if not admin_class:
                admin_class = 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__)
                if not model._meta.swapped:
                    if options:
                        options['__module__'] = __name__
                        admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
                    self._registry[model] = admin_class(model, self)
     admin.site.register(models.UserGroup,admin.ModelAdmin)

      register操作完成以后,生成了一个这样的字典:

    {
                 models.UserInfo: UserInfoAdmin(models.UserInfo,site对象)[add,change..],
                 models.UserGroup: ModelAdmin(models.UserGroup,site对象),}

      然后回到urls.py文件中,生成对应的路由映射关系:

    urlpatterns = [
        url(r'^nb/', v1.site.urls),
    ]
  • 相关阅读:
    react setstate
    【css】长文本左侧显示省略号
    react 获取input标签的输入值
    【react 分页器】 基于react-virtualized组件的分页器
    type为number的<input>标签 type和size属性失效
    js 获取Array数组 最大值 最小值
    ubuntu 编辑pdf
    【react redux && flux】
    远程唤醒、WOL、Magic_Packet
    win10无法访问局域网共享文件?(因微软账户和本地账户登陆问题导致)
  • 原文地址:https://www.cnblogs.com/vipchenwei/p/7691409.html
Copyright © 2011-2022 走看看