• day-72Django源码解析


    settings源码

      用户配置了settings用用户的,没有配置用默认的

    ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"          #设置全局大字典
    
    from django.conf import settings
    
    class Settings(object):
        def __init__(self, settings_module):                 # BBS.settings
                
            for setting in dir(global_settings):             # 拿到global_settings文件里面所有的变量名
                if setting.isupper():  
                    setattr(self, setting, getattr(global_settings, setting))
            self.SETTINGS_MODULE = settings_module
    
            mod = importlib.import_module(self.SETTINGS_MODULE)
            
            
             for setting in dir(mod):                       #  获取暴露给用户settings配置中的变量名
                if setting.isupper():
                    setting_value = getattr(mod, setting)
                    setattr(self, setting, setting_value)   # 利用字典的键存在与否 完成用户配置了用用户的,用户没配置用全局
    
        
    class LazySettings(LazyObject):
         def _setup(self, name=None):
          
            settings_module = os.environ.get(ENVIRONMENT_VARIABLE)  # 从全局大字典os.environ中获取一个键为ENVIRONMENT_VARIABLE对应的值
            self._wrapped = Settings(settings_module)
    
    settings = LazySettings()

    admin启动源码

      django在启动的时候会依次执行每一个应用下的admin.py文件

    from django.utils.module_loading import autodiscover_modules
    autodiscover_modules('admin')

    admin注册源码

    class ModelAdmin(BaseModelAdmin):
            ...
            # 配置类
            
            
        class AdminSite(object):
            def __init__(self, name='admin'):
                self._registry = {}  
            def register(self, model, admin_class=None, **options):
                
                if not admin_class:
                    admin_class = ModelAdmin
                
                self._registry[model] = admin_class(model)
        
        site = AdminSite()
            
        admin.py:
            admin.site.register(models.Publish)  # 仅仅是将注册了的模型表和以模型表为参数实例化产生的对象
                                                 # 当做键值对存入了site对象中的_registry字段

    django admin的使用

        1.在应用下注册你的模型表
        
        2.admin url的规律
            http://127.0.0.1:8000/admin/app01/book/                book表的查看
            http://127.0.0.1:8000/admin/app01/book/add/            book表的添加
            http://127.0.0.1:8000/admin/app01/book/3/change/        book表的编辑
            http://127.0.0.1:8000/admin/app01/book/3/delete/        book表的删除页面
            
            http://127.0.0.1:8000/admin/app01/publish/              publish表的查看
            http://127.0.0.1:8000/admin/app01/publish/add/          publish表的添加
            http://127.0.0.1:8000/admin/app01/publish/3/change/     publish表的编辑
            http://127.0.0.1:8000/admin/app01/publish/3/delete/     publish表的删除页面
        
            ps:
                1.admin会给每一个注册了的生成增删改查四条url
            
            
        3.五大关键性参数的功能

    路由分发的本质

      url(r'^test/',([],None,None))

    def get_urls(self):
            urlpatterns = [
                url(r'^$', wrap(self.index), name='index'),
                url(r'^login/$', self.login, name='login'),
                url(r'^logout/$', wrap(self.logout), name='logout'),
                url(r'^password_change/$', wrap(self.password_change, cacheable=True), name='password_change'),
                url(r'^password_change/done/$', wrap(self.password_change_done, cacheable=True),
                    name='password_change_done'),
                url(r'^jsi18n/$', wrap(self.i18n_javascript, cacheable=True), name='jsi18n'),
                url(r'^r/(?P<content_type_id>d+)/(?P<object_id>.+)/$', wrap(contenttype_views.shortcut),
                    name='view_on_site'),
            ]
            return urlpatterns
        
        @property
        def urls(self):
            return self.get_urls(), 'admin', self.name
        
        
        
        
        一级分发
        url(r'^index/',([
                url(r'^test1/',test1),
                url(r'^test2/',test2),
                        ],None,None))
                        
        二级分发
            url(r'^index/',([
                url(r'^test1/',([
                        url(r'^test1_1/',test3),
                        url(r'^test1_2/',test4),
                        url(r'^test1_3/',test5),
                        url(r'^test1_4/',test6),
                                ],None,None)),
                url(r'^test2/',test2),
                        ],None,None))

    单例模式

    基于classmethod:

    基于装饰器的:

    基于元类__call__:

    基于__new__:

    基于模块的:

      模块的导入只会执行一次,所以实现了单例

  • 相关阅读:
    字付串函数 substring(ex,star,end)
    OBJECT_ID临时表无效
    行列转换
    字符串类型系统函数
    exec sql
    系统函数 1
    常见排序算法-----简单选择排序
    排序概念------排序的稳定性
    JAVA学习-参数传递
    常见排序算法-----二分插入排序
  • 原文地址:https://www.cnblogs.com/klw1/p/11279822.html
Copyright © 2020-2023  润新知