73. django setting configuration summary, djangosetting

Source: Internet
Author: User
Tags name database

73. django setting configuration summary, djangosetting

In the previous article, 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 the app name is not added by default, for example, 'app1' # Add new apps here]

2. Database Configuration

If you use the default sqlite3 database of django, you do not need to change it.

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.sqlite3',        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),    }}

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

DATABASES = {'default': {'Engine ': 'django. db. backends. mysql ', 'name': 'blog', # Your database NAME database needs to be created in advance 'user': 'root', # Your database username 'Password ': '', # Your Database Password 'host':'', # Your Database HOST, default localhost 'Port': '123', # Your Database PORT }}

In addition, you need to add

import pymysqlpymysql.install_as_MySQLdb()

For more information, see: http://www.cnblogs.com/liluning/p/7729607.html

3. SQL statements

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 operations are related to the database, our written statements are translated into SQL statements and printed on the server.

4. static file directory

STATIC_URL = '/static/' # Call directory STATICFILES_DIRS = [OS. path. join (BASE_DIR, "static"), # path]

5. If UserInfo (User table) in the database inherits django built-in AbstractUser

1) model needs to be imported

from django.contrib.auth.models import AbstractUser

2) configuration file

AUTH_USER_MODEL = "Application name. UserInfo"

6. Middleware

Self-written middleware, such as M1 and M2 in the md. py file under the md folder in 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 self-written middleware. The configuration should be written after the system.

7,Configuration of session Storage

1) database configuration (default)

Django supports sessions by default, and stores Session data in the database by default, that is, the django_session table. Configure settings. py SESSION_ENGINE = 'django. contrib. sessions. backends. db' # Engine (default) SESSION_COOKIE_NAME = "sessionid" # Session cookie key stored in the browser, that is, sessionid = random string (default) SESSION_COOKIE_PATH = "/" # path for saving Session cookies (default) SESSION_COOKIE_DOMAIN = None # domain name for storing Session cookies (default) SESSION_COOKIE_SECURE = False # Whether to transmit cookies over Https (default) SESSION_COOKIE_HTTPONLY = True # Whether Session cookies only Support http transmission (default) SESSION_COOKIE_AGE = 1209600 # Session cookie expiration date (2 weeks) (default) SESSION_EXPIRE_AT_BROWSER_CLOSE = False # Whether to disable the browser to make the Session expire (default) SESSION_SAVE_EVERY_REQUEST = False # Whether to save the Session for each request. It is saved only after modification by default (default)
View Code

2) cache Configuration

Configure settings. py SESSION_ENGINE = 'django. contrib. sessions. backends. cache '# Engine SESSION_CACHE_ALIAS = 'default' # The cache alias used (default memory cache, or memcache ), here, the alias depends on the cache settings SESSION_COOKIE_NAME = "sessionid" # The Session cookie key stored in the browser, that is: sessionid = random string SESSION_COOKIE_PATH = "/" # Session's cookie storage path SESSION_COOKIE_DOMAIN = None # Session's cookie storage domain name SESSION_COOKIE_SECURE = False # Https transmission cookie SESSION_COOKIE_HTTPONLY = True # Session? the cookie only supports http transmission of 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 to save the Session for each request. It is saved only after modification by default.
View Code

3) Default Configuration

Configure settings. py SESSION_ENGINE = 'django. contrib. sessions. backends. file' # Engine SESSION_FILE_PATH = None # cache file path. If it is None, use the tempfile module to obtain a temporary address tempfile. gettempdir () SESSION_COOKIE_NAME = "sessionid" # key of Session cookie stored in the browser, that is: sessionid = random string SESSION_COOKIE_PATH = "/" # Session's cookie storage path SESSION_COOKIE_DOMAIN = None # Session's cookie storage domain name SESSION_COOKIE_SECURE = False # Https transmission cookie SESSION_COOKIE_HTTPONLY = True # Session? the cookie only supports http transmission of 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 to save the Session for each request. It is saved only after modification by default.
View Code

Note:

1) You can also customize the configuration, but you can import the configuration when writing the custom configuration to the final code of the configuration file.

from django.conf import settings
Settings. Configuration name

2) All the above configurations are system default configurations that need to be modified for specific problems.

3) The above configuration is just the common configurations that are encountered in the previous django series. Subsequent configurations will be added and updated continuously in this article.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.