Django中settings.py含义

"""
Django settings for myWeb project.

Generated by 'django-admin startproject' using Django 2.2.1.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g%%zeo)i74zdyv##-+%c1!8y0-mxjgw!bq9sw8=2!cvf9gpwma'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

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

ROOT_URLCONF = 'myWeb.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'myWeb.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }
# 取消使用sqlite3,改为使用mysql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_1',
        'USER': 'root',
        'PASSWORD': '123456',
        # 'PASSWORD': 'mysql123456',
        'HOST': '127.0.0.1',
        'POST': 3306,
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

# 添加静态文件 static
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static'),
]
参数 含义
BASE_DIR 项目路径
SECRET_KEY 密钥配置,项目创建时自动生成的随机值,主要用于用户密码、CSRF机制和会话Session等数据加密
DEBUG 如果在开发阶段应设为 True,在开发调试过程中会自动检测代码是否发生更改,根据检测结果执行是否刷新系统;部署上线则置为 False,防止泄露项目相关信息
ALLOWED_HOSTS 设置可访问域名
当 DEBUG = True 且 ALLOWED_HOSTS 为空时只允许 localhost 或 127.0.0.1 访问
当 DEBUG = False 且 ALLOWED_HOSTS 为必填项,否则程序无法启动
ALLOWED_HOSTS = [’*’]为允许所有域名访问
INSTALLED_APPS APP列表
admin,内置的后台管理系统
auth,内置用户认证系统
contenttypes,记录项目中所有model元数据(Django的ORM框架)
sessions,Session会话功能,记录相关用户信息
messages,消息提示功能
staticfiles,查找静态资源路径
自定义app,python manage.py startapp xxx
MIDDLEWARE 中间件是一个用来处理Django请求和响应的框架钩子,开发者可根据自己的开发需求定义中间件
SecurityMiddleware,内置的安全机制
SessionMiddleware,会话session功能
CommonMiddleware,处理请求信息规范化请求内容
CsrfViewMiddleware,开启CSRF防护功能
AuthenticationMiddleware,开启内置的用户认证系统
MessageMiddleware,开启内置信息提示功能
XFrameOptionsMiddleware,防止恶意程序单击挟持
ROOT_URLCONF
TEMPLATES 模板时一种特殊的HTML文档,这个HTML文档能够嵌入一些能让Django识别的变量和指令,然后由Django的模板引擎解析变量指令
BACKEND,用于识别模板里的变量和指令
DIRS,设置模板路径
APP_DIRS,是否在APP里查找模板文件
OPTIONS用于填充在RequestContext的上下文
WSGI_APPLICATION
DATABASES 数据库
AUTH_PASSWORD_VALIDATORS
上一篇:一些好看的前端图表库


下一篇:镜像里日志文件中文不能展示展示的问题