一个课程分多个章节,一个章节分多个视频。在前端遍历所有的章节,在章节下遍历所有视频。Video有个外建lesson,lesson有个外键course。
class Course(models.Model):
def get_course_lesson(self):
"""
获取课程所有章节
"""
return self.lesson_set.all()
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_name_plural = verbose_name
def __str__(self):
return self.name
def get_lesson_video(self):
"""
获取章节视频
"""
return self.video_set.all()
class Video(models.Model):
lesson = models.ForeignKey(Lesson, verbose_name="章节")
name = models.CharField(max_length=100, verbose_name="视频名")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
url = models.CharField(max_length=200,default='',verbose_name='访问地址')
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟数)")
class Meta:
verbose_name = "视频"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
这样就可以在前端调用了。{% for lesson in course.get_course_lesson %}、
{% for video in lesson.get_lesson_video %}。
然后是资料下载。也类似。
<div class="box mb40">
<h4>资料下载</h4>
<ul class="downlist">
{% for course_resource in course_resources%}
<li>
<span ><i class="aui-iconfont aui-icon-file"></i> {{ course_resource.name }}</span>
<a href="{{ MEDIA_URL }}{{ course_resource.download }}" class="downcode" target="_blank" download="" data-id="274" title="">下载</a>
</li>
{% endfor %}
</ul>
</div>