Python Day 71 Django framework setting source code analysis, realization setting file in the user's exposure to custom configure it to use the user-configured based on the source principle, no configuration is to use the global default

  ## Django framework setting source code analysis

= ENVIRONMENT_VARIABLE " DJANGO_SETTINGS_MODULE "   # This is a global big dictionary 
from django.conf Import Settings # Step 1: Check the source django global setting entry 

class Settings (Object): # The fourth step
     DEF  __init__ (Self, settings_module):   # BBS.settings 
        # Update from the this dict, Ltd. Free Join Settings (But only for ALL_CAPS Settings) 
        for Setting in dir (global_settings):   # get global_settings file inside all variable names 
            IF setting.isupper ():   # configuration file can only write uppercase variable names
                setattr (Self, Setting, getattr (global_settings, Setting)) 
        self.SETTINGS_MODULE = settings_module 

        MOD = importlib.import_module (self.SETTINGS_MODULE) # dynamic import module, the incoming string 
        # from the BBS Import Settings 
        
         for Setting in the dir (MOD ):   # Get the name of the user is exposed to a variable configuration settings 
            IF setting.isupper (): 
                setting_value = getattr (MOD, Setting) 
                setattr (Self, Setting, setting_value)   # using the presence or absence of the key dictionary is configured by the user to complete the user the user is not configured with a global 

    
class LazySettings (LazyObject): # step 3: Check how the implementation process
     DEF _setup (Self, name = None):
         "" " 
        . the Load at The Settings Module pointed to by at The Environment variable This 
        IS Used at The First Time WE need the any Settings AT All, IF at The the User has not 
        of Previously the Configured at The Settings Manually. 
        " " " 
        settings_module = os.environ.get (ENVIRONMENT_VARIABLE)   # get a ENVIRONMENT_VARIABLE the corresponding value for the key from the global os.environ large dictionary, the value of where to look? in 
                                                                # will be able to see the beginning of os.environ manage.py file .setdefault ( "DJANGO_SETTINGS_MODULE", "day72.settings") 
        self._wrapped = Settings (settings_module) 

Settings = LazySettings ()    # Step two: create a new object

 

Guess you like

Origin www.cnblogs.com/liangzhenghong/p/11276782.html