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

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),
]

Django项目启动之前执行流程剖析的更多相关文章

  1. 开发必备知识点--django项目启动时,url加载之前,执行某个.py文件

    django项目启动时,自定义执行某个py文件 在任意的app下的apps.py中的Config类下自定义ready()方法,并且调用autodiscover_modules. app01/apps. ...

  2. Spring Security Oauth2 单点登录案例实现和执行流程剖析

    Spring Security Oauth2 OAuth是一个关于授权的开放网络标准,在全世界得到的广泛的应用,目前是2.0的版本.OAuth2在“客户端”与“服务提供商”之间,设置了一个授权层(au ...

  3. Springboot 项目启动后执行某些自定义代码

    Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...

  4. Jedis cluster命令执行流程剖析

    Jedis cluster命令执行流程剖析 在Redis Cluster集群模式下,由于key分布在各个节点上,会造成无法直接实现mget.sInter等功能.因此,无论我们使用什么客户端来操作Red ...

  5. 在web项目启动时执行某个方法

    在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...

  6. SpringMVC启动和执行流程

    Spring框架大家用得很多,相当熟悉,但是我对里面的运作比较好奇,例如bean的加载和使用,和我们定义的配置文件有什么联系;又例如aop在什么时候起作用,原理又是怎样.经过一个了解后,整理了启动和执 ...

  7. Django 中 admin 的执行流程

    Django 中 admin 的执行流程 1 循环加载执行所有已经注册的 app 中的 admin.py 文件 def autodiscover(): autodiscover_modules('ad ...

  8. Spring Security 案例实现和执行流程剖析

    Spring Security Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication ...

  9. Spring Boot学习--项目启动时执行指定service的指定方法

    Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...

随机推荐

  1. weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )

    ~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...

  2. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

    问题: 当我们打开数据库,即use dbname时,要预读数据库信息,当使用-A参数时,就不预读数据库信息. 解决方法:mysql -hhostname -uusername -ppassword - ...

  3. Dream------Hadoop--FSDataInputStream和FSDataOutputStream

    一.FSDataInputStream    FileSystem中的open()方法实际上返回的是一个FSDataInputStream,而不是标准的java.io类.这个类是java.io.Dat ...

  4. 【算法学习】有旋treap

    treap是平衡树的一种.与其他平衡树一样,它也能够支持插入和删除,求第k极值等,接下来我们主要探讨有旋treap的实现过程. treap中每个节点要维护其值,左右孩子以及子树大小.父亲要不要写则看你 ...

  5. sicily 1215. 脱离地牢

    Description 在一个神秘的国度里,年轻的王子Paris与美丽的公主Helen在一起过着幸福的生活.他们都随身带有一块带磁性的阴阳魔法石,身居地狱的魔王Satan早就想得到这两块石头了,只要把 ...

  6. linux之发送邮件--sendmail服务配置

    新手入门也不知道什么日志分析服务好,鸟哥说logwatch,那我就从logwatch开始吧! logwatch用到了emai发邮件,先从配置邮件发送sendmail开始: 安装sendmail服务,我 ...

  7. C# Guid 16位 唯一

    public static class GuidExtentions { /// <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> ...

  8. JVM性能调优监控工具——jps、jstack、jmap、jhat、jstat、hprof使用详解

    摘要: JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jstack.jmap.jhat.jstat.hprof等小巧的工具,本博客希望 ...

  9. 基于docker 搭建Prometheus+Grafana

    一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...

  10. Linux学习笔记:输入输出重定向 >>命令

    Linux重定向是指修改原来默认的一些东西,对原来系统命令的默认执行方式进行改变.比如说我不想看到在显示器的输出,而是希望输出到某一文件中就可以通过Linux重定向来进行这项工作. 将stdout重定 ...