"""
Django settings for backend project.
Generated by 'django-admin startproject' using Django 3.2.4.
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/
"""from pathlib import Path
import os
import sys
# Build paths inside the project like this: BASE_DIR / 'subdir'.# BASE_DIR = Path(__file__).resolve().parent.parent
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))print(BASE_DIR)# 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-l+hg)43b0u5(#@(c(@_f8&nf(4ln+(9d((kdw97jq1d(s)jk#u'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG =True
ALLOWED_HOSTS =[]# Application definition
sys.path.insert(0, os.path.join(BASE_DIR,'apps'))
INSTALLED_APPS =['simpleui',# 系统的子应用'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',# 自定义的子应用'content.apps.ContentConfig','user.apps.UserConfig',# 第三方的子应用'rest_framework',# drf'wangeditor',# 富文本编辑器]
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 ='backend.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 ='backend.wsgi.application'# Database# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES ={'default':{'ENGINE':'django.db.backends.mysql','HOST':'127.0.0.1','PORT':3306,# 数据库端口'USER':'root',# 数据库用户名'PASSWORD':'root',# 数据库用户密码'NAME':'blog'# 数据库名字}}# 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/
LANGUAGE_CODE ='zh-Hans'
TIME_ZONE ='Asia/Shanghai'
USE_I18N =True
USE_L10N =True
USE_TZ =False# 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''''在django认证系统中配置自定义的模型类'''
AUTH_USER_MODEL ='user.User'# 应用名.模型类名
from django.apps import AppConfig
classUserConfig(AppConfig):
default_auto_field ='django.db.models.BigAutoField'
name ='user'
verbose_name ='A_用户管理'
blog
from django.apps import AppConfig
classContentConfig(AppConfig):
default_auto_field ='django.db.models.BigAutoField'
name ='content'
verbose_name ='B_内容管理'
六、admin
user
from django.contrib import admin
from user.models import User
@admin.register(User)classUserAdmin(admin.ModelAdmin):
list_display =['id','username','mobile','signature','last_login','date_joined','avatar',]
blog
from django.contrib import admin
from content.models import Category, Article
@admin.register(Category)classCategoryAdmin(admin.ModelAdmin):
list_display =['id','name',]@admin.register(Article)classArticleAdmin(admin.ModelAdmin):
list_display =['id','title','category_id','author_id','create_time','modified_time']
七、views
user
from django.shortcuts import render
# Create your views here.
"""backend URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""from django.contrib import admin
from django.urls import path, include
urlpatterns =[
path('admin/', admin.site.urls),
path('content/', include('content.urls'))]
user
hhh
blog
from django.urls import path
from content.views import ArticleAPIview
urlpatterns =[
path('article/', ArticleAPIview.as_view())]
九、模板
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>博客</title></head><body>{%for k, v in res.items %}{{k}}:{{v}}{% endfor %}</body></html>