1. 新建courses app
在manage.py命令行输入startapp courses
2. 规划courses的表
需要有一个表记录各个课程名称等信息, 每个课程需要有一个表记录各个章节信息, 每个章节需要有一个表记录各节课时的视频信息, 因此需要设计三个表.
另外, 每个课程需要下载一些辅助资源, 因此, 还需要加辅助资源表.
3. 编写courses的models
from datetime import datetime
from django.db import models
class Course(models.Model):
name = models.CharField(max_length=50, verbose_name="课程名")
desc = models.CharField(max_length=300, verbose_name="课程描述")
detail = models.TextField(verbose_name="课程详情") # 暂时定义成textfield, 后续会改为支持富文本的类型
degree = models.CharField(choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟数")
students = models.IntegerField(default=0, verbose_name="学习人数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
image = models.ImageField(upload_to="courses/%Y/%m", verbose_name="封面图", max_length=100)
click_nums = models.IntegerField(default=0, verbose_name="点击数")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程"
verbose_name_plural = verbose_name
class Lesson(models.Model):
course = models.ForeignKey(Course, verbose_name="课程")
name = models.CharField(max_length=100, verbose_name="章节名")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "章节"
verbose_n