展示课程
展示课程
-
.创建course模型
-
创建用户模型course
-
python ../manage.py startapp course # 创建course模型
-
-
在setting.py中注册course模型(第一步)
-
INSTALLED_APPS = [ 'apps.course.apps.CourseConfig', ]
-
-
syl/urls.py添加路由(第二步)
-
urlpatterns = [ path('course/', include('course.urls')), ]
-
-
4 创建 course/urls.py路由文件(第三步)
-
from django.urls import path, re_path from . import views urlpatterns = [ ]
-
-
-
syl/utils/MyBaseModel.py增加抽象基 类
-
""" author:翔翔 date: use: """ from django.db import models 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
-
-
.课程主页面3张表
-
所需包
-
from django.db import models from utils.MyBaseModel import Base
-
-
分类表
-
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
-
-
-
course/admin.py注册后台管理
-
from django.contrib import admin from . import models admin.site.register(models.CourseType) admin.site.register(models.CourseTag) admin.site.register(models.Course) admin.site.register(models.Sections) admin.site.register(models.Chapters)
-
迁移表
-
python manage.py makemigrations python manage.py migrate
-
-
-
增加media文件配置
-
syl/settings.py
-
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # #/teach/shiyanlou_project/syl/media
-
-
syl/urls.py
-
from django.conf.urls.static import static from syl import settings urlpatterns = [ ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
-
-
放一个测试图片到 syl/media/1.jpg 中
-
http://192.168.56.100:8888/media/1.jpg
-
-
-
接口开发
-
course/urls.py 添加路由
-
""" author:翔翔 date: use: """ from . import views from rest_framework.routers import DefaultRouter router = DefaultRouter() # 有根路由 router.register(r'type', views.CourseTypeViewSet) # 写路由 router.register(r'tag', views.CourseTagViewSet) router.register(r'course', views.CourseViewSet) urlpatterns = [ ] urlpatterns += router.urls # 把两个路由列表相加
-
-
course/serializers.py 写序列化器
-
from rest_framework import serializers from .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 SectionsSerializer(serializers.ModelSerializer): class Meta: model = Sections fields = '__all__' class ChaptersSerializer(serializers.ModelSerializer): sections = SectionsSerializer(many=True) class Meta: model = Chapters fields = '__all__' class CourseDeepSerializer(CourseSerializer): # 字段名名, 必须是模型可以 . 引用到的变量 # Course(). "chapters" 才能作为字段名, 如果是集合, 需要加many=True, chapters = ChaptersSerializer(many=True)
-
-
course/views.py 添加视图函数
-
from .serializers import * from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets from rest_framework.filters import OrderingFilter 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 = CourseDeepSerializer # 指定过滤方法类, 排序方法类, 一个或多个 filter_backends = (DjangoFilterBackend, OrderingFilter) # 同时支持过滤和排序 # 指定排序字段, 不设置, 排序功能不起效 ordering_fields = ('attention', 'learner', 'create_time') # 指定过滤字段, 不设置, 过滤功能不起效 filter_fields = ('title', 'desc', 'status', 'course_type', 'course_tag') # 自定义分页器 覆盖全局配置 pagination_class = PageNum def get_serializer_class(self): if self.action == 'list': return CourseSerializer else: return CourseDeepSerializer class SectionsViewSet(viewsets.ModelViewSet): queryset = Sections.objects.all() serializer_class = SectionsSerializer
-
-
接口
-