09.后台主页应用
home应用的创建
注册drf、弃用jdango的csrf
# dev.py
INSTALLED_APPS = [
# ...
'rest_framework',
]
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',
'corsheaders.middleware.CorsMiddleware',
]
创建 home 应用
"""
1.前往apps目录下创建app:cd luffyapi/apps
2.创建app:python ../../manage.py startapp home
"""
配置 apps 到环境变量并注册home应用
# dev.py
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(BASE_DIR, 'apps'))
# 注:不添加环境变量,注册 应用 很麻烦,会出现 应用 找不到
INSTALLED_APPS = [
# ...
'home'
]
注册子路由
# 主路由:urls.py
from django.urls import include
urlpatterns = [
# ...
path('home/', include('home.urls')),
]
# 子路由:home/urls.py
from django.urls import path, re_path
urlpatterns = [
]