配置文件如下,下面对配置文件进行一一解释
"""
Django settings for film1_manager project.
Generated by 'django-admin startproject' using Django 2.2.
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__)))
#BASE_DIR=film_manager
# 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 = '^-fz_k8_eam%axnvh3@tqd7wjbzfx31*ac3e40ue^)jyaopb2n'
# 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',
'film'
]
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 = 'film1_manager.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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 = 'film1_manager.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'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'filmdatabase',
'USER': 'zilv',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '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 = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
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/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
1、BASE_DIR: 当前⼯程的根⽬录,Django会依此来定位⼯程内的相关⽂件,我们也可以使⽤该参数来构造⽂件路径。
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2. DEBUG:调试模式,创建⼯程后初始值为True,即默认⼯作在调试模式下。
作⽤:
- 修改代码⽂件,程序⾃动重启
- Django程序出现异常时,向前端显示详细的错误追踪信息
- ⽽⾮调试模式下,仅返回Server Error (500)
3. 本地语⾔与时区
Django⽀持本地化处理,即显示语⾔与时区⽀持本地化。
本地化是将显示的语⾔、时间等使⽤本地的习惯,这⾥的本地化就是进⾏中国化,中国⼤陆地区使⽤简体中⽂,时区使⽤亚洲/上海时区,注意这⾥不使⽤北京时区表示。
初始化的⼯程默认语⾔和时区为英语和UTC标准时区
LANGUAGE_CODE = 'en-us' # 语⾔
TIME_ZONE = 'UTC' # 时区# 时区
将语⾔和时区修改为中国⼤陆信息
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
4.静态⽂件:STATIC_URL、STATICFILES_DIRS
项⽬中的css、image、js都是静态⽂件。⼀般会将静态⽂件放到⼀个单独的⽬录中,以⽅便管理。在html⻚⾯中调⽤时,也需要指定静态⽂件的路径,Django中提供了⼀种解析的⽅式配置静态⽂件路径。静态⽂件可以放在项⽬根⽬录下,也可以放在应⽤的⽬录下,由于有些静态⽂件在项⽬中是通⽤的,所以推荐放在项⽬的根⽬录下,⽅便管理。
为了提供静态⽂件,需要配置两个参数:
STATICFILES_DIRS 存放查找静态⽂件的⽬录
STATIC_URL 访问静态⽂件的URL前缀
例如:
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
5、ALLOWED_HOSTS = []:允许访问的ip地址加入到列表中,如果在启动服务指定了ip,python manage.py runserver 192.168.17.6,需要将192.168.17.6加入到ALLOWED_HOSTS中
6、INSTALLED_APPS:将创建的子应用添加到INSTALLED_APPS中,可以直接添加子应用名称或者子应用名称.apps.FilmConfig
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'film'
]
7、MIDDLEWARE:项目中用到的一些中间件
8、ROOT_URLCONF = ‘film1_manager.urls’:项目的根路由
9、TEMPLATES:项目中定义的模板文件,需要配置模板路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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',
],
},
},
]
10、DATABASES:项目中的数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'filmdatabase',
'USER': 'zilv',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
11、AUTH_PASSWORD_VALIDATORS:用户经常选择错误的密码。为了帮助缓解此问题,Django提供了可插入的密码验证。您可以同时配置多个密码验证器。Django中包含一些验证器,但是编写自己的验证器也很简单。