1. Load the database, the database configuration can not be written dead in the 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 base_dir generally use the default values, namely:
Base_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))
The specific action is to create a config folder, the folder and your project package peer, and then create a Config.json file below it, the file content is as follows:
{
"Db_user": "Root",
"db_pwd": "123456",
"Db_host": "127.0.0.1",
"Db_port": "3306"
}
In the above code, the engine to illustrate, the official provides a different database of different operating engines, the following is 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. Time zone settings and character set settings, generally used are the following three:
Set the Save to database time type to be UTC time, if not required, set to false, default = True:use_tz = True
Set time zone: Time_zone = ' Asia/shanghai '
Setting language: Language_code = ' Zh-hans '
Set CharSet: Default_charset = "Utf-8"
3. Set internationalization, set according to individual needs:
Internationalization: use_i18n = True
Whether the same content is displayed in different formats when accessed by users in different time zones (for example, time, date, number): use_l10n = True
4. Deployment configuration:
Debug mode is turned on: debug = True
To access the IP configuration of the Web service: allowed_hosts = [' * ']
When deploying, modify the values of Debug and allowed_hosts, and generally modify them to false and specify IP, for example [' 127.0.0.1 ']
5. Define a global variable in setting.py, the variable name of which needs to be all uppercase, otherwise it will not be referenced:
Contextbool= False
6.app installation configuration, that is, the Installed_apps setting, our new project needs to be added to this configuration, the meaning of each configuration is explained later:
django.contrib.admin--manage the site.
django.contrib.auth--certification System.
django.contrib.contenttypes--the framework used for content types.
The session data can be viewed in the Django_session table in the database django.contrib.sessions--the conversation frame.
django.contrib.messages--the message frame.
django.contrib.staticfiles--the framework for managing static files.
For example:
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, that is, the middleware settings, the so-called middleware is from the user request to the end of the user request to do the operation, that is, the user's request will be executed from the top to the bottom of the middleware configuration, and then the server responds to the user again from bottom to top execution , similar to Java's filter:
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 ',
]
General Django Configuration as above, if you need to add your own middleware configuration, you can add yourself, there is no more to say.
Encrypted salts in 8.setting:
Secret_key = ' i&&2$s&#%7npev^ #uix ==kis+h$4$ozscefiaw1c%p^+1c (l&6 '
This configuration is a Django security configuration that is used to prevent attacks, which are generated by an algorithm of the system when startproject.
The content configuration of setting is roughly the same, and the role of each configuration is explained in a later article.