Source resolve day-72Django

Source settings

  Users configure the settings with the user, not configured with default

= ENVIRONMENT_VARIABLE " DJANGO_SETTINGS_MODULE "           # Set the global big dictionary 

from django.conf Import Settings 

class Settings (Object):
     DEF  __init__ (Self, settings_module):                  # BBS.settings 
            
        for Setting in dir (global_settings):              # get all the documents inside global_settings variable names 
            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):                        #   get exposed to the user settings configuration variable name 
            IF setting.isupper (): 
                setting_value = getattr (MOD, Setting) 
                setattr (Self, Setting, setting_value)    # presence or absence of key completed using the dictionary with the user's user profile, the user is not configured with the global 

    
class LazySettings (LazyObject):
      DEF the _setup (Self, name = None): 
      
        settings_module = os.environ.get (ENVIRONMENT_VARIABLE)   # from the global Dictionary of os.environ acquires a key corresponding to a value of ENVIRONMENT_VARIABLE 
        self._wrapped = Settings (settings_module)

settings = LazySettings()

admin start Source

  django executes admin.py files in each application at startup order

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

admin registered source

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)  # Only the registered model in the model table and the parameter table of the generated instance object 
                                             # as a key site for the field into the object _registry

The use django admin

    1 . Register your application in the model table
    
     2 rule .admin url of 
        HTTP: //127.0.0.1:8000/admin/app01/book/                 Book Table View 
        HTTP: //127.0.0.1:8000/admin/app01/ book / add /             add book tables 
        HTTP: //127.0.0.1:8000/admin/app01/book/3/change/         book a table editor 
        HTTP: //127.0.0.1:8000/admin/app01/book/3/ the delete /         Book table delete a page 
        
        http: //127.0.0.1:8000/admin/app01/publish/               View publish tables 
        http: //127.0.0.1:8000/admin/app01/publish/add/           add publish tables 
        http : //127.0.0.1:8000/admin/app01/publish/3/change/     publish table editor 
        HTTP: //127.0.0.1:8000/admin/app01/publish/3/delete/      delete pages publish table 
    
        PS:
             1 .Admin will give each registered generate CRUD four url
        
        
     3. Five function key parameters

 

 

The nature of the route distribution

  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__:

基于模块的:

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

Guess you like

Origin www.cnblogs.com/klw1/p/11279822.html