syl/settings.py 配置
INSTALLED_APPS = [ 'course.apps.CourseConfig', ]
主路由配置
from django.contrib import admin from django.urls import path,include from django.conf.urls.static import static from syl import settings urlpatterns = [ path('course/', include('course.urls')), ] urlpatterns +=static(settings.MEDIA_URL,document_root=setting
子路由配置
from rest_framework.routers import DefaultRouter from . import views router=DefaultRouter() router.register(r'type',views.CourseTypeViewSet) router.register(r'tag',views.CourseTagViewSet) router.register(r'course',views.CourseViewSet) router.register(r'sections',views.SectionsViewSet) router.register(r'chapters',views.ChaptersViewSet) urlpatterns = [ ] urlpatterns +=router.urls
models.py 中配置
from django.db import models # Create your models here. # from utils.MyBaseModel import Base class Base(models.Model): create_time=models.DateTimeField('创建时间',auto_now_add=True,null=True) update_time=models.DateTimeField('更新时间',auto_now=True,null=True) class Meta: abstract=True class CourseType(Base): title=models.CharField('课程类别',max_length=16) sequence=models.IntegerField('展示顺序',default=10) class Meta: db_table='tb_coursetype' def __str__(self): return self.title class CourseTag(Base): title=models.CharField('课程标签',max_length=16) sequence=models.IntegerField('展示顺序',default=10) class Meta: db_table='tb_coursetag' def __str__(self): return self.title class Course(Base): STATUS=( ('0','即将上线'), ('1','已经上线'), ('2','已下线'), ) title=models.CharField('课程名',max_length=24) desc=models.CharField('课程描述',max_length=256) img=models.ImageField('课程logo',upload_to='course',null=True) course_type=models.ForeignKey(CourseType,verbose_name='课程类别',on_delete=models.SET_NULL,default=None,null=True) course_tag=models.ManyToManyField(CourseTag,verbose_name='课程标签') status=models.CharField('课程状态',choices=STATUS,max_length=8,default='1') attention=models.IntegerField('关注人数',default=0) learner=models.IntegerField('学习人数',default=0) class Meta: db_table='tb_course' def __str__(self): return self.title class Chapters(Base): title=models.CharField('章标题',max_length=24) serial_num=models.IntegerField('章序号') course=models.ForeignKey(Course,related_name='chapters',on_delete=models.SET_NULL,null=True) class Meta: db_table='tb_chapters' def __str__(self): return self.title class Sections(Base): title=models.CharField('节标签',max_length=24) serial_num=models.IntegerField('节序号') chapters=models.ForeignKey(Chapters,related_name='sections',on_delete=models.SET_NULL,null=True) learn_time=models.IntegerField('学习小时',default=1) video=models.FileField("上传视频",upload_to="videos/%y%m%d/",blank=True,max_length=1024) seq_num=models.IntegerField("序号",default=1) class Meta: db_table='tb_sections' def __str__(self): return self.title
admin.py 中配置
from django.contrib import admin # Register your models here. from .import models admin.site.register(models.CourseType) admin.site.register(models.CourseTag) admin.site.register(models.Course) admin.site.register(models.Chapters) admin.site.register(models.Sections)
serializers.py中配置
from rest_framework import serializers from course.models import * class CourseTypeSerializer(serializers.ModelSerializer): class Meta: model=CourseType fields="__all__" class CourseTagSerializer(serializers.ModelSerializer): class Meta: model=CourseTag fields="__all__" class CourseSerializer(serializers.ModelSerializer): class Meta: model=Course fields="__all__" class ChaptersSerializer(serializers.ModelSerializer): class Meta: model=Chapters fields="__all__" class SectionsSerializer(serializers.ModelSerializer): class Meta: model=Sections fields="__all__" class CourssesDeepSerializer(CourseSerializer): chapters=CourseSerializer(many=True)
views 中配置
from django.shortcuts import render # Create your views here. from rest_framework import viewsets from rest_framework.pagination import PageNumberPagination from course.models import * from course.serializers import * class PageNum(PageNumberPagination): page_size_query_param = 'page-size' class CourseTypeViewSet(viewsets.ModelViewSet): queryset = CourseType.objects.all() serializer_class=CourseTypeSerializer class CourseTagViewSet(viewsets.ModelViewSet): queryset = CourseTag.objects.all() serializer_class=CourseTagSerializer class CourseViewSet(viewsets.ModelViewSet): queryset=Course.objects.all() serializer_class=CourseSerializer class SectionsViewSet(viewsets.ModelViewSet): queryset = Sections.objects.all() serializer_class=SectionsSerializer class ChaptersViewSet(viewsets.ModelViewSet): queryset = Chapters.objects.all() serializer_class = ChaptersSerializer