73. Summary of setting configuration of django



In the previous essay, we often change the setting configuration and often confuse some configurations. Today, we mainly summarize some common configurations.

 

Setting configuration summary

1. app path

 

  INSTALLED_APPS = [

    'djanGO.contrib.admin',

    'djanGO.contrib.auth',

    'djanGO.contrib.contenttypes',

    'djanGO.contrib.sessions',

    'djanGO.contrib.messages',

    'djanGO.contrib.staticfiles',

    'app1.apps.App1Config',  

    # If it is already available by default, just add the app name. For example: 'app1'

    # New applications should be added here

]

 

2. Database configuration

If you use djanGO The default sqlite3 database does not need to be changed

 

  DATABASES = {

    'default': {

        'ENGINE': 'djanGO.db.backends.sqlite3',

        'NAME': os.path.





 

If you use the mysql database, you need to note out the above database and modify it as follows

 

  DATABASES = {

    'default': {        

        'ENGINE': 'djanGO.db.backends.mysql',

        'NAME': 'blog', #Your database name The database needs to be your own Build 'USER' in advance

        : 'root', #your database username

        'PASSWORD': '', #your database password

        'HOST': '', #your database host, leave it blank and default to localhost

        'PORT' : '3306', #Your database port

    }

}

 

and you need to add

 

  import pymysql

pymysql.install_as_MySQLdb() to the application's __init__.py file. For

 

details, see: http://www.cnblogs.com/liluning/p/7729607. html

3. sql statement

 

  LOGGING = {

    'version': 1,

    ' disable_existing_loggers': False,

    'handlers': {

        'console':{

            'level':'DEBUG',

            'class':'logging.StreamHandler',

        },

    },

    'loggers': {

        'djanGO.db.backends': {

            'handlers': ['console'],

            'propagate': True,

            'level': 'DEBUG',

        },

    }

} 

 

When your operation is related to the database, it will translate our written statement into sql statement and print it on the server side.

4. Static file directory

 

  STATIC_URL = '/static/' #When calling the directory



STATICFILES_DIRS=[

    os.path.join(BASE_DIR,"static"), #specific path

]

 

5. If the UserInfo (user table) in the database inherits djanGO Built-in AbstractUser

1) model needs to be imported

 

  from djanGO.contrib.

 



 

  AUTH_USER_MODEL = "Application Name.UserInfo"

 

6. Middleware Middleware written by

yourself , such as M1 and M2 in the md.py file in the md folder of the project

 

  MIDDLEWARE = ​​[

    'djanGO.middleware.security .SecurityMiddleware',

    'djanGO.contrib.sessions.middleware.SessionMiddleware',

    'djanGO.middleware.common.CommonMiddleware',

    'djanGO.middleware.csrf.CsrfViewMiddleware',

    'djanGO.contrib.auth.middleware.AuthenticationMiddleware',

    'djanGO .contrib.messages.middleware.MessageMiddleware',

    'djanGO.middleware.clickjacking.XFrameOptionsMiddleware',

    'md.md.M1',

    'md.md.M2',

]

 

Pay attention to the middleware written by yourself, the configuration should be written in the back of the system

7. Related configuration of session storage

1) Database configuration (default)



  

   Django supports Session by default, and the default is to store Session data in the database, namely: djanGO_session table.

 

Configure settings.py

 

    SESSION_ENGINE = 'djanGO.contrib.sessions.backends.db' # Engine (default)

     

    SESSION_COOKIE_NAME = "sessionid" # The key when the session cookie is saved on the browser, namely: sessionid=random string (default )

    SESSION_COOKIE_PATH = "/" # Session cookie save path (default)

    SESSION_COOKIE_DOMAIN = None # Session cookie save domain name (default)

    SESSION_COOKIE_SECURE = False # Whether Https transmits cookies (default)

    SESSION_COOKIE_HTTPONLY = True # Whether session cookie Only supports http transmission (default)

    SESSION_COOKIE_AGE = 1209600 # Session cookie expiration date (2 weeks) (default)

    SESSION_EXPIRE_AT_BROWSER_CLOSE = False # Whether to close the browser to make the Session expire (default)

    SESSION_SAVE_EVERY_REQUEST = False # Whether to save the Session for each request, and save it after the default modification (default)

  

  View Code

 

2) Cache configuration



  

    configuration settings.py

 

    SESSION_ENGINE = 'djanGO.contrib .sessions.backends.cache' # Engine

    SESSION_CACHE_ALIAS = 'default' # The cache alias used (default memory cache, or memcache), where the alias depends on the cache setting

 

 

    SESSION_COOKIE_NAME = "sessionid" # Session cookies are saved in the browser The key on the server, namely: sessionid = random string

    SESSION_COOKIE_PATH = "/" # The path where the session's cookie is saved

    SESSION_COOKIE_DOMAIN = None # The domain name where the session's cookie is saved

    SESSION_COOKIE_SECURE = False # Whether Https transmits cookies

    SESSION_COOKIE_HTTPONLY = True # Whether session cookies only support http transmission

    SESSION_COOKIE_AGE = 1209600 # Session cookie expiration date (2 weeks)

    SESSION_EXPIRE_AT_BROWSER_CLOSE = False # Whether to close the browser to make the session expire

    SESSION_SAVE_EVERY_REQUEST = False # Whether every Session is saved for each request,

  

  View Code is saved after default modification

 

3) Default configuration



  

   configuration settings.py

 

    SESSION_ENGINE = 'djanGO.contrib.sessions.backends.file' # Engine

    SESSION_FILE_PATH = None # Cache file path, if None, use The tempfile module gets a temporary address tempfile.gettempdir()       

    SESSION_COOKIE_NAME = "sessionid" # The key when the session's cookie is saved on the browser, namely: sessionid = random string

    SESSION_COOKIE_PATH = "/" # The path where the session's cookie is saved

    SESSION_COOKIE_DOMAIN = None # The domain name where the session's cookie is saved

    SESSION_COOKIE_SECURE = False # Whether Https transmits cookies

    SESSION_COOKIE_HTTPONLY = True # Whether session cookies only support http transmission

    SESSION_COOKIE_AGE = 1209600 # Session cookie expiration date (2 weeks)

    SESSION_EXPIRE_AT_BROWSER_CLOSE = False # Whether to close the browser to make the session expire

    SESSION_SAVE_EVERY_REQUEST = False # Whether every The session is saved for each request, and it is saved after the default modification.

  

  View Code

 

Note:

1) You can also customize the configuration, but the customized configuration must be written in the final code of the configuration file. When using it, you can import the configuration

 

  from djanGO.conf import settings

  settings. Configuration name

 

2) All the above configurations are for specific problems The default configuration of the system that needs to be modified will not be explained

3) The above configuration is just the common configuration encountered in the previous djanGO series essays. The subsequent configuration will be gradually added and updated in this essay.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326365044&siteId=291194637