Django--4--子路由的配置
首先我们了解一下settings下面的各个文件的作用
"""
Django settings for hhPro project.
Generated by 'django-admin startproject' using Django 3.2.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
# 返回一个项目的绝对路径
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
#数据加密,防止网站被跨域
SECRET_KEY = 'django-insecure-jwo+!0%#hmhk40*q=(dpoo28qz0ik^t5#fju#ejerpsg1*f^8t'
# SECURITY WARNING: don't run with debug turned on in production!
#是否开启debug模式,该网站处于开发者模式下
DEBUG = True
#网站访问白名单,[]里面输入访问的ip,全部访问可以['*']
ALLOWED_HOSTS = []
# Application definition
#应用注册,比如我这里就加了----- 'api',
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
]
#中间件
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 = 'hhPro.urls' 为默认路由
ROOT_URLCONF = 'hhPro.urls'
#ROOT_URLCONF = 'api.urls'
#配置html静态文件
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 = 'hhPro.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
#配置数据库
DATABASES = {
#'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'goodsOrder',
'USER': 'root',
'PASSWORD': 'yinchenxi@9999',
'HOST': '101.133.174.190',
'PORT': 3306,
}
}
# Password validation
# https://docs.djangoproject.com/en/3.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/3.2/topics/i18n/
#网站默认语言 也可以改成汉字---- 'zh-hans'
LANGUAGE_CODE = 'en-us'
#默认系统时间 改成上海时间----- 'ASia/shanghai'
TIME_ZONE = 'UTC'
#
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
然后我们在视图层增加一个函数
然后在我们的子路由配置我们函数内容
主路由配置允许访问的主路由地址
然后启动
python manage.py runserver 0.0.0.0:8001
第一次访问
第二次访问
备注:需注意就是配置多个子路由需要在主路由进行导入,并且每个子路由下面的访问接口路径不能存在重复
沫笙