python based tutorial: Django framework of setting.py file Detailed description

This article introduces the Django framework in the Detailed Description setting.py file, this file contains all the configuration information about the Django project, it has a certain reference value, small partners who are interested can refer to
1. Load database configuration database can not write died in seting.py file, the following way is to read another file, configuration database:

config = ''
with open(os.path.join(BASE_DIR, 'config/config.json'), 'rt') as f:
  config = json.load(f)
  
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'szrqgl',
    'USER': config['db_user'],
    'PASSWORD': config['db_pwd'],
    'HOST': config['db_host'],
    'POST': config['db_port']
  }
}

The above is the default value BASE_DIR generally used, namely:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Specific action is to create a config folder, the folder and you project this package at the same level, and then create a file in its config.json below document reads as follows:

{
 "db_user": "root",
 "db_pwd": "123456",
 "db_host": "127.0.0.1",
 "db_port": "3306"
}

In the above code, ENGINE to explain, the official operation of the different engines for different databases, the following are a few frequently used:

sqlite database: 'django.db.backends.sqlite3',

postgreSQL database: 'django.db.backends.postgresql_psycopg2',

mysql database: 'django.db.backends.mysql'

oracle database: 'django.db.backends.oracle'

2. Set the time zone and the character set, generally used are the following three:

Settings are saved to the database type is time to UTC time, if you do not set to false, default is true: USE_TZ = True

Set the time zone: TIME_ZONE = 'Asia / Shanghai'

Set Language: LANGUAGE_CODE = 'zh-hans'

Character Set: DEFAULT_CHARSET = "utf-8"

3. Set international, set according to individual needs:

Internationalization: USE_I18N = True

The same content is not simultaneously access the user area of ​​the region, whether to show content in different formats (such as time, date, number): USE_L10N = True

4. deployment configuration:

Debug mode is enabled: DEBUG = True

Ip access the web service configuration: ALLOWED_HOSTS = [ '*']

Deployment time and change the value in DEBUG ALLOWED_HOSTS generally modified as FALSE and IP specify, for example, [ '127.0.0.1']

5. Define the global variables in setting.py, the variable name of the variable needs of all caps, otherwise it will not quote:

CONTEXTBOOL= False

6.app installation configuration, namely INSTALLED_APPS set, we need to add new items to this configuration, the meaning of each configuration will be described later:

django.contrib.admin - management site.

django.contrib.auth - authentication system.

django.contrib.contenttypes - for the content type of the frame.

django.contrib.sessions - frame session, session data may be viewed in a database table django_session.

django.contrib.messages - message frame.

django.contrib.staticfiles - framework for managing static files.

E.g:

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'demo1',
]

7.django middleware configuration, i.e. MIDDLEWARE disposed, is called a middleware request from the user to the operation request made during the end user, i.e. the user's request will be performed sequentially from top to bottom views MIDDLEWARE configuration, then the server response to the user time will again perform in order from bottom to top, and Java Filter is very similar:

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',
]

Django general configuration as shown above, if you need to add your own middleware configuration, can also add your own, I will not say any more.

The encryption 8.setting salt:

SECRET_KEY = 'i&&2$s&#%7npev^#uix==kis+h$4$ozscefiaw1c%p^+1c(l&6'

This configuration is the security configuration django prevent attacks with, the value is generated by an algorithm startProject system.

9. static files directory configuration (most important)
all static directory files are placed in the configuration nginx when static resource nginx to point to here.

STATIC_ROOT = os.path.join(BASE_DIR, 'static').replace('\\', '/')

Alias, this alias is the name cited in html

STATIC_URL = '/commonstatic/'

For example, the following is actually static commonstatic, which is shown below in html inside:

{% block styles %}
  <link rel="stylesheet" href="{% static '/plugins/bootstrapValidator/bootstrapValidator.min.css' %}" rel="external nofollow" >
{% endblock %}

Here Insert Picture Description
Specify the directory location of the static files in django

STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'commonstatic/').replace('\\', '/'),
)

Here Insert Picture Description
In general, the above-described static files almost universal above configuration, the absolute need to reprovision few cases.

10.session session configuration (following configuration is saved in django_session database):

SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 引擎(默认)
SESSION_COOKIE_NAME = "sessionid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
SESSION_COOKIE_PATH = "/" # Session的cookie保存的路径(默认)
SESSION_COOKIE_DOMAIN = None # Session的cookie保存的域名(默认)
SESSION_COOKIE_SECURE = False # 是否Https传输cookie(默认)
SESSION_COOKIE_HTTPONLY = True # 是否Session的cookie只支持http传输(默认)
SESSION_COOKIE_AGE = 60 * 30 # Session的cookie失效日期(30min)(默认)
SESSION_EXPIRE_AT_BROWSER_CLOSE = True # 是否关闭浏览器使得Session过期(默认)
SESSION_SAVE_EVERY_REQUEST = True # 是否每次请求都保存Session,默认修改之后才保存(默认)

content setting profile substantially as the specific role of each configuration will be explained in a future article!
Finally, we recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience, study notes, there is a chance of business experience, and calmed down to zero on the basis of information to project combat , we can at the bottom, leave a message, do not know to put forward, we will study together progress

Published 22 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun08/article/details/104740808