Python站
  • 说明
  • 源码分析
  • 100 系列
  • python
    • 开发基础
      • 数据类型
      • 字符编码
      • 文本操作
      • 玩转Python100解
    • 函数编程
      • 装饰器
      • 内置函数
        • vars
      • 内置模块
        • enum
        • os
        • argparse
        • importlib
        • shelve
        • collections
        • re
      • 神奇三方
        • setuptools
        • celery
    • 面向对象
      • 面向对象100解(草稿)
    • 网络编程
      • 网络理论知识
      • 网络编程socket
      • socketserver源码泛读
    • 并发编程
      • 并发编程之多进程
        • 子进程基本使用
        • 进程的属性或方法
        • 守护进程
        • 互斥锁
        • 队列
        • 生产者消费者模型
      • 并发编程之多线程
        • 子线程基本使用
        • GIL全局解释器锁
        • 死锁现象与递归锁
        • 信号量事件定时器
        • 线程队列queue
      • 并发编程之进程线程池
      • 并发编程之协程
      • 并发编程之IO模型
        • 阻塞IO
        • 非阻塞IO
        • 多路复用IO
        • 异步IO
        • IO模型比较分析
    • 数据库说
      • mongoengine源码精读
    • 前端开发
    • 后端开发
      • flask源码精读
      • django源码精读
        • 01 软件打包与部署
        • 02 python调试工具 pdb
        • 03 创建一个django项目
    • 爬虫相关
      • Jupyter Notebook
      • urllib
      • urllib3源码泛读
      • 数据解析
      • requests源码精读
        • 初始化说 __init__.py
        • 版本信息 __version__.py
        • 版本兼容 compat.py
        • 经典视图 api.py
        • 逻辑实现 sessions.py
        • 数据存储 models.py
        • 网络传输 adapters.py
        • 异常结构 exceptions.py
        • 交互协定 status_code.py
        • 钩子编程 hooks.py
        • 数据结构 structures.py
        • 辅助输出 utils.py
        • 安全认证 auth&certs.py
    • 编程思想
      • 算法
      • 数据结构
      • 设计模式
        • 简单工厂模式
        • 工厂方法模式
        • 抽象工厂模式
        • 建造者模式
        • 单例模式
        • 适配器模式
        • 桥模式
        • 组合模式
        • 外观模式
        • 代理模式
        • 责任链模式
        • 观察者模式
        • 策略模式
        • 模板方法模式
      • Python技巧100解
      • Effective Python
    • 企业应用
      • DevOps
      • Web服务-Nginx
      • 网站发布
      • 源码管理
        • Git
        • GitHub
        • GitLab
      • Golang
      • Docker
      • Ubuntu
    • 项目实战
    • 就业相关
    • 其他爱好
      • 科技单词100解答
Powered by GitBook
On this page
  • 源码分析
  • 总结经验
  1. python
  2. 后端开发
  3. django源码精读

03 创建一个django项目

源码分析

django-admin startproject project1执行的主干逻辑代码

# django/bin/django-admin.py
from django.core import management
if __name__ == '__main__':
    management.execute_from_command_line()


# django/core/management/__init__.py
@functools.lru_cache(maxsize=None)    # 函数级别缓存, 666
def get_commands():
    commands = {name: 'django.core' for name in find_commands(__path__[0])}
    return commands

def load_command_class(app_name, name):
    # 如何根据动态的名字字符串拼接导入路径,import_module
    module = import_module('%s.management.commands.%s' % (app_name, name))
    return module.Command()

class ManagementUtility:
    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
    def fetch_command(self, subcommand):
        commands = get_commands()
        app_name = commands[subcommand]    # 'djanog.core'
        klass = load_command_class(app_name, subcommand)    # 'startproject'
        return klass
    def execute(self):
        subcommand = self.argv[1]    # startproject
        self.fetch_command(subcommand).run_from_argv(self.argv)

def execute_from_command_line(argv=None):
    utility = ManagementUtility(argv)
    utility.execute()
    

# django/core/management/commands/startproject.py
class Command(TemplateCommand):
    def handle(self, **options):
        project_name = options.pop('name')
        target = options.pop('directory')
        options['secret_key'] = get_random_secret_key()
        super().handle('project', project_name, target, **options)
# django/core/management/templates.py
class TemplateCommand(BaseCommand):
    def handle(self, app_or_project, name, target=None, **options):
        '''模板文件拷贝'''
# django/core/management/base.py
class BaseCommand:
    def run_from_argv(self, argv)
        self.execute(*args, **cmd_options)
    def execute(self, *args, **options):
        output = self.handle(*args, **options)
        return output
    def handle(self, *args, **options):
        raise NotImplementedError()

总结经验

  • 对外提供接口,适合以函数方式实现,execute_from_command_line

  • class ManagementUtility: 适合处理解析参数,分发处理

  • 结合目录 commands,实现动态导入

  • BaseCommand - TemplateCommand - Comand,典型的接口-实现模式

Previous02 python调试工具 pdbNext爬虫相关

Last updated 5 years ago