页面右边教师数的显示,teacher有个外键叫org,连接到表CourseOrg,而Course表有个外键叫course_org连接到CourseOrg。于是可以在CourseOrg里
def get_teacher_nums(self):
#获取机构教师数量
return self.teacher_set.all().count()
于是在前端调用该机构教师数时
{{ course.course_org.get_teacher_nums }}
前端调用时不能写括号,不能调用需要传参数的方法。
接下来是相关课程。给每个课程增加一个字段,有相同字段的课程是相关课程。
class CourseDetailView(View):
"""
课程详情页
"""
def get(self,request,course_id):
course= Course.objects.get(id=course_id)
course.click_nums +=1
course.save()
has_fav_course = False
has_fav_org = False
if request.user.is_authenticated():
if UserFavorite.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
has_fav_course = True
if UserFavorite.objects.filter(user=request.user, fav_id=course.course_org.id, fav_type=2):
has_fav_org = True
tag = course.tag
if tag:
relate_courses = Course.objects.filter(tag=tag)[:1]
else:
relate_courses =[]
return render(request,"course-detail.html",{
"course":course,
"relate_courses":relate_courses,
"has_fav_course":has_fav_course,
"has_fav_org":has_fav_org
})
然后是收藏功能,这里的收藏和之前不完全一样,不需要从post获取数据
网友评论