django app注册过程
相关知识
sys.modules[__name__]获取本模块
collections是Python内建的一个集合模块,提供了许多有用的集合类。
namedtuple
namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素。
这样一来,我们用namedtuple可以很方便地定义一种数据类型,它具备tuple的不变性,又可以根据属性来引用,使用十分方便。
deque
使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为list是线性存储,数据量大的时候,插入和删除效率很低。
deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈
defaultdict
使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict
OrderedDict
使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。
Counter
Counter是一个简单的计数器,例如,统计字符出现的个数:

字符串函数rpartition与partition
这两个函数都接收一个分割字符串作为参数,将目标字符串分割为两个部分,返回一个三元元组(head,sep,tail),包含分割符。细微区别在于前者从目标字符串的末尾也就是右边开始搜索分割符。

 def setup(set_prefix=True):"""Configure the settings (this happens as a side effect of accessing thefirst setting), configure logging and populate the app registry.Set the thread-local urlresolvers script prefix if `set_prefix` is True."""from django.apps import appsfrom django.conf import settingsfrom django.urls import set_script_prefixfrom django.utils.encoding import force_textfrom django.utils.log import configure_loggingconfigure_logging(settings.LOGGING_CONFIG, settings.LOGGING)if set_prefix:set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else force_text(settings.FORCE_SCRIPT_NAME))apps.populate(settings.INSTALLED_APPS)#注册app
 class Apps(object):"""A registry that stores the configuration of installed applications.It also keeps track of models eg. to provide reverse-relations."""def __init__(self, installed_apps=()):# installed_apps is set to None when creating the master registry# because it cannot be populated at that point. Other registries must# provide a list of installed apps and are populated immediately.if installed_apps is None and hasattr(sys.modules[__name__], 'apps'):raise RuntimeError("You must supply an installed_apps argument.")# Mapping of app labels => model names => model classes. Every time a# model is imported, ModelBase.__new__ calls apps.register_model which# creates an entry in all_models. All imported models are registered,# regardless of whether they're defined in an installed application# and whether the registry has been populated. Since it isn't possible# to reimport a module safely (it could reexecute initialization code)# all_models is never overridden or reset.self.all_models = defaultdict(OrderedDict)# Mapping of labels to AppConfig instances for installed apps.self.app_configs = OrderedDict()# Stack of app_configs. Used to store the current state in# set_available_apps and set_installed_apps.self.stored_app_configs = []# Whether the registry is populated.self.apps_ready = self.models_ready = self.ready = False# Lock for thread-safe population.self._lock = threading.Lock()# Maps ("app_label", "modelname") tuples to lists of functions to be# called when the corresponding model is ready. Used by this class's# `lazy_model_operation()` and `do_pending_operations()` methods.self._pending_operations = defaultdict(list)# Populate apps and models, unless it's the master registry.if installed_apps is not None:self.populate(installed_apps)def populate(self, installed_apps=None):"""Loads application configurations and models.This method imports 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# app_config should be pristine, otherwise the code below won't# guarantee that the order matches the order in INSTALLED_APPS.if self.app_configs:raise RuntimeError("populate() isn't reentrant")# Phase 1: initialize app configs and import app modules.for entry in installed_apps:if isinstance(entry, AppConfig):app_config = entryelse: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_configapp_config.apps = self#改变了自身,完成注册可以通过apps管理# 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@classmethod#工厂方法def create(cls, entry):"""Factory that creates an app config from an entry in INSTALLED_APPS."""try:# If import_module succeeds, entry is a path to an app module,# which may specify an app config class with default_app_config.# Otherwise, entry is a path to an app config class or an error.module = import_module(entry)except ImportError:# Track that importing as an app module failed. If importing as an# app config class fails too, we'll trigger the ImportError again.module = Nonemod_path, _, cls_name = entry.rpartition('.')# Raise the original exception when entry cannot be a path to an# app config class.if not mod_path:raiseelse:try:# If this works, the app module specifies an app config class.entry = module.default_app_configexcept AttributeError:# Otherwise, it simply uses the default app config class.return cls(entry, module)else:mod_path, _, cls_name = entry.rpartition('.')# If we're reaching this point, we must attempt to load the app config# class located at <mod_path>.<cls_name>mod = import_module(mod_path)try:cls = getattr(mod, cls_name)except AttributeError:if module is None:# If importing as an app module failed, that error probably# contains the most informative traceback. Trigger it again.import_module(entry)else:raise# Check for obvious errors. (This check prevents duck typing, but# it could be removed if it became a problem in practice.)if not issubclass(cls, AppConfig):raise ImproperlyConfigured("'%s' isn't a subclass of AppConfig." % entry)# Obtain app name here rather than in AppClass.__init__ to keep# all error checking for entries in INSTALLED_APPS in one place.try:app_name = cls.nameexcept AttributeError:raise ImproperlyConfigured("'%s' must supply a name attribute." % entry)# Ensure app_name points to a valid module.try:app_module = import_module(app_name)except ImportError:raise ImproperlyConfigured("Cannot import '%s'. Check that '%s.%s.name' is correct." % (app_name, mod_path, cls_name,))# Entry is a path to an app config class.return cls(app_name, app_module)

通过模块名,类函数静态方法创建工程函数,

django app注册过程相关推荐

  1. [Django]APP级别的静态文件处理

    2019独角兽企业重金招聘Python工程师标准>>> 转载自 limodou的学习记录 [Django]APP级别的静态文件处理 静态文件在 django 中并不是非常简单的事情. ...

  2. First Django APP

    个人学习笔记,参考django官方文档:https://docs.djangoproject.com/zh-hans/3.2/ 本文同步发表在我的个人博客上:https://sunguoqi.com/ ...

  3. Django - app

    1.app目录 migrations:数据库操作的记录(只记录修改表结构的记录); __init__文件:python2中必须填加,python3中无要求,可以不添加: admin:django为我们 ...

  4. django app中扩展users表

    app models中编写新的User 1 # _*_ coding:utf-8 _*_2 from __future__ import unicode_literals34 from django. ...

  5. django app服务器搭建

    首先是在ubuntu 16.04下进行搭建的,开发工具选用aptana,python选用python3.5,django选用最新的版本进行搭建,最后搭建完毕后发现数据库配置一直有问题,所以最后直接在w ...

  6. Heroku 部署 Django APP

    什么是Heroku? Heroku是一个支持多种编程语言的云平台, 可以理解为一个免费的托管服务器.开发者开发完app,使用Git程序推送到Heroku的Git服务器上,这样其他人就可以通过网址来访问 ...

  7. Django|编写第一个Django程序|Writting your first Django app|Windows环境

    目录 1.Django官网:The web framework for perfectionists with deadlines | Django 2.django的安装 一.未安装 二.已安装dj ...

  8. 【0】依照Django官网:实现第一个django app 安装必要的工具/模块

    1.环境配置: (1)Install Setuptools¶ To install Python packages on your computer, Setuptools is needed. Do ...

  9. 【AMAD】django-filer -- 一个管理文件和图片的django app

    动机 简介 个人评分 动机 django-filer1可以让你像一些云存储一样使用WEB UI控制你的文件. 简介 下面是前端图片:   个人评分 类型 评分 实用性 ⭐️⭐️⭐️⭐️ 易用性 ⭐ ...

最新文章

  1. iOs 自定义UIView 日历的实现 Swift2.1
  2. ci框架——辅助函数
  3. iOS从零开始学习直播之音频2.后台播放和在线播放
  4. 关于 DOM 操作的几个类型
  5. 图卷积网络进行骨骼识别代码_【骨骼行为识别】2s-AGCN论文理解
  6. Centos7 虚拟机复制后网卡问题 Job for network.service failed
  7. 零基础学Python-爬虫-5、下载音频
  8. gbase迁移mysql_基于datax实现从gbase到mysql的数据迁移--时间字段篇
  9. linux对硬盘进行分区吗,Linux下如何对硬盘进行分区
  10. C++:成绩类Score
  11. QT设置相对路径最简单方法
  12. nfc和红外线的区别_红外,蓝牙,WiFi,NFC区别及优缺点
  13. 计算机组成原理实验四 微程序控制器实验报告
  14. 前端页面设置重置按钮或刷新按钮
  15. 使用hardhat开发以太坊智能合约-测试合约
  16. Android开发 设置手机壁纸
  17. 奥斯汀页眉怎么设置_word红头文件怎么制作
  18. Codeforces Round #750 (Div. 2)E. Pchelyonok and Segments (数学+DP)
  19. Cannot add property xxx, object is not extensible
  20. 异常处理 --- 一些垂死挣扎的代码

热门文章

  1. Python基础之进程(Process)
  2. 新手开水果店到底赚不赚钱,开水果店一年赚钱大概有多少
  3. Android JNI 详解
  4. js实现replaceAll方法
  5. java的不足_java的优势和不足
  6. python画图小猪佩奇_python海龟作图20秒完成小猪佩奇,附源码!
  7. 第四节 基本命令和程序结构控制(1)
  8. MIL-STD-1553B (GJB-289A)
  9. 【NuMaker-M2354试用】MicroSD 模块测评
  10. 蚂蚁区块链第12课 如何使用命令行编译工具solcjs编译Solidity智能合约?