文章目录
项目结构概述
当我们使用django-admin startproject project-name
创建项目时,即自动创建该名称的目录,目录结构如下:
yutdeMacBook-Pro:django_test yut$ django-admin startproject web
yutdeMacBook-Pro:django_test yut$ ls # django_test目录下生成web目录
web
yutdeMacBook-Pro:django_test yut$ tree web
web # 与项目名称同名目录
├── manage.py # 自动生成的管理主程序
└── web # 与项目名称同名目录
├── __init__.py
├── asgi.py
├── settings.py # 配置项
├── urls.py # url网址配置
└── wsgi.py
1 directory, 6 files
settings.py
- settings.py 包含了django项目启动的所有配置项
- 配置项分为公有配置和自定义配置:公有配置为django官方提供基础配置
- 配置项格式:BASE_DIR = ‘XXX’
- 自定义配置名称尽量个性化防止覆盖公有配置,例如ALIPAY_KEY = ‘xxx’
- 引入方式:from django.conf import settings
可以通过vscode,打开目录的方式浏览项目结构和文件内容:
- settings.py 具体内容解析
"""
Django settings for web project.
Generated by 'django-admin startproject' using Django 3.0.6.
For more information on this file, see(更多配置信息内容)
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see(更全的配置以及值设置信息)
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os # 调用os操作系统模块,便于python与系统进行对接操作,例如创建目录、文件
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR) # /Users/yut/Desktop/django_test/web
# os.path.abspath(__file__)表示当前文件即settings.py的绝对路径
# os.path.dirname表示或取该文件所在目录,一次一级,两次套娃则表示上上一级目录
# 实际上上述代码是获得了该项目所在的目录,并将该目录作为基础目录
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3^r4gvw*ugin8pjzsmdvvhnvurj4+0beotc0=e$d394t#=8cdh'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# True调试模式,在开发测试使用,检测代码改动后立刻重启服务,具备报错页面,可以根据报错信息进行debug
# False上线部署模式,正式部署网页的时候使用,防止暴露后台信息
# 请求host头,f12查看网页host头,django允许接入访问的网址/域名,默认空配
# ['www.baidu.com'] 具体域名
# ['*'],允许在局域网其他主机也能访问此主机的django服务,启动时python3 manage.py runserver 0.0.0.0:5000
# 指定网络设备如果内网环境下其他主机想访问改站点,需要ALLOWED_HOSTS = ['内网ip'],其他设备通过该域名:端口号访问
# []空列表表示请求头host为127.0.0.1,localhost能访问本项目
# ['192.168.0.1', '127.0.0.1'] 表示只有这两个host可以访问
ALLOWED_HOSTS = ['*'] # 允许网页可以被访问到的域名
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'web.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 = 'web.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.0/topics/i18n/
# 语言,中文'zh-Hans',英文'en-us'
LANGUAGE_CODE = 'zh-Hans'
# 时区,东八区'Asia/Shanghai',格林尼治'UTC'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'