def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) course.students +=1 course.save() # 查询用户是否已经学习了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 这里不用 # user_courses.user = request.user # user_courses.course = course # 因为 user,course 是外键,在 UserCourse 实际上存储的是 id ,这些 id 是已经存在的 user_courses = UserCourse(user=request.user, course=course) user_courses.save() # 得出学过该课程的同学还学过的课程 user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_course.course.id for user_course in all_user_courses] relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) # 通过外键找到章节再找到视频对应的课程 course = video.lesson.course course.students += 1 course.save() # 查询用户是否已经学习了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 如果没有学习该门课程就关联起来 user_course = UserCourse(user=request.user, course=course) user_course.save() # 相关课程推荐 # 找到学习这门课的所有用户 user_courses = UserCourse.objects.filter(course=course) # 找到学习这门课的所有用户的id user_ids = [user_course.user_id for user_course in user_courses] # 通过所有用户的id,找到所有用户学习过的所有过程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [all_user_course.course_id for all_user_course in all_user_courses] # 通过所有课程的id,找到所有的课程,按点击量去五个 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5] # 资源 all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-play.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, 'video': video, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.courses course.students += 1 course.save() # 查询用户是否已经关联该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() user_cousers = UserCourse.objects.filter(course=course) user_ids = [user_couser.user.id for user_couser in user_cousers] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [user_couser.course.id for user_couser in all_user_courses] # 获取学过该用户学过其他的课程 relate_courses = Courses.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course = course) return render(request, 'course-play.html', { 'course':course, 'course_resources':all_resources, 'relate_courses':relate_courses, 'video':video })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) course.students += 1 course.save() # 查询用户是否已经学习了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 如果没有学习该门课程就关联起来 user_course = UserCourse(user=request.user, course=course) user_course.save() # 相关课程推荐 # 找到学习这门课的所有用户 user_courses = UserCourse.objects.filter(course=course) # 找到学习这门课的所有用户的id user_ids = [user_course.user_id for user_course in user_courses] # 通过所有用户的id,找到所有用户学习过的所有过程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [all_user_course.course_id for all_user_course in all_user_courses] # 通过所有课程的id,找到所有的课程,按点击量取前3 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:3] # 资源 all_resources = CourseResource.objects.filter(course=course) return render(request, "course-video.html", { "course": course, "all_resources": all_resources, "relate_courses": relate_courses, })
def get(self, request, course_id): # 此处的id为表默认为我们添加的值。 course = Course.objects.get(id=int(course_id)) # 查询用户是否开始学习了该课,如果还未学习则,加入用户课程表 user_courses = UserCourse.objects.filter(user= request.user, course= course) if not user_courses: user_course = UserCourse(user= request.user, course= course) course.students +=1 course.save() user_course.save() # 查询课程资源 all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系 user_courses = UserCourse.objects.filter(course= course) # 从关系中取出user_id user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的课程,外键会自动有id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [user_course.course_id for user_course in all_user_courses] # 获取学过该课程用户学过的其他课程 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums").exclude(id =course.id)[:4] # 是否收藏课程 return render(request, "course-video.html", { "course": course, "all_resources": all_resources, "relate_courses":relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) course.students += 1 course.save() # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [user_course.course.id for user_course in all_user_courses] # 获取该用户学过的其他课程 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5] all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-video.html', { 'course': course, 'course_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_courses = UserCourse(user=request.user, course=course) user_courses.save() user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_course.course.id for user_course in all_user_courses] relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-play.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, 'video': video, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) #查询用户是否已经关联了本课程 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse(user=request.user, course=course) user_course.save() all_resources = CourseResource.objects.filter(course=course) all_comment = CourseComments.objects.filter(course=course) user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_course.user.id for user_course in all_user_courses] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:3] return render( request, 'course-comment.html', { 'course': course, 'all_resources': all_resources, 'all_comment': all_comment, 'relate_courses': relate_courses })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) course.click_nums += 1 course.save() comments = CourseComment.objects.filter(course=course) # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() course.student += 1 course.save() # 学习过该课程的所有同学 user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_courses = UserCourse.objects.filter( user_id__in=user_ids).order_by('-course__click_nums')[:5] # related_courses = [user_course.course for user_course in all_courses if user_course.course.id != course.id] related_courses = [] for item in all_courses: if item.course.id != course.id: related_courses.append(item.course) course_resources = CourseResource.objects.filter(course=course) return render( request, 'course-comment.html', { 'course': course, 'course_resources': course_resources, 'related_courses': related_courses, 'comments': comments, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) #点击数 course.students += 1 course.save() #查询用户是否学习了该教程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() #相关课程推荐 #找到学习这门课的所有用户 user_courses = UserCourse.objects.filter(course=course) #找到学习这门课的所有用户id user_ids = [user_course.user_id for user_course in user_courses] #通过所有用户的id,找到所有用户学习过的课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) #取出所有课程id course_ids = [ all_user_course.course_id for all_user_course in all_user_courses ] #通过所有课程的id,找到所有的课程,按点击量取五个 relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] #课程资源 all_resources = CourseResource.objects.filter(course=course) return render( request, 'course/course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id): # 根据前端传来的课程id,查找出课程 course = Course.objects.get(id=int(course_id)) course.students += 1 course.save() # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() # 根据课程,查出所有学过这门课程的用户 user_courses = UserCourse.objects.filter(course=course) # 使用python列表推导式查出所有用户的id user_ids = [user_course.user.id for user_course in user_courses] # 根据用户的id找出用户还学过的其它课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) #然后查出相应课程的id course_ids = [us_course.course.id for us_course in all_user_courses] # 最后根据课程id,查询出学习该课程的用户还学过其它的课程 relater_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums")[:5] # course_resc = course.courseresource_set.all() # 这两种查询方式得到的结果一样 course_resc = CourseResource.objects.filter(course=course) return render( request, 'course-video.html', { 'course': course, 'course_resc': course_resc, 'relater_courses': relater_courses, })
def get(self, request, course_id): courses = Course.objects.get(id=int(course_id)) courses.click_nums += 1 courses.save() #用户与课程进行关联 user_course = UserCourse.objects.filter(course=courses) if not user_course: user_course = UserCourse(user=request.user, course=courses) user_course.save() has_fav_course = False has_fav_org = False if request.user.is_authenticated: if UserFavorite.objects.filter(user=request.user, fav_id=courses.id, fav_type=1): has_fav_course = True if UserFavorite.objects.filter(user=request.user, fav_id=courses.course_org.id, fav_type=2): has_fav_org = True relate_course = [] tag = courses.tag if tag: tcourse = Course.objects.filter(tag=tag).order_by("-click_nums") for t in tcourse: if t.id != int(course_id): relate_course.append(t) break return render( request, 'course-detail.html', { 'courses': courses, 'relate_course': relate_course, 'has_fav_course': has_fav_course, 'has_fav_org': has_fav_org, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course # 查询用户是否关联了此课程 has_course = UserCourse.objects.filter(user=request.user, course=course) if not has_course: has_course = UserCourse(user=request.user, course=course) has_course.save() # 学习人数加1 course.students += 1 course.save() # 获取学过此课程所有用户 user_courses = UserCourse.objects.filter(course=course) # 获取学过此课程所有用户的ID user_ids = [user_course.user_id for user_course in user_courses] # 获取学过此课程所有用户的课程 all_users_courses = UserCourse.objects.filter(user_id__in=user_ids) # 获取学过此课程所有用户学习过的其他所有课程的ID course_ids = [ user_course.course.id for user_course in all_users_courses if user_course.course.id != int(video.lesson.course.id) ] # 获取学过此课程所有用户学习过的其他所有课程 relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] course_resources = CourseResource.objects.filter(course=course) return render( request, 'course-play.html', { 'course': course, 'course_resources': course_resources, 'relate_courses': relate_courses, 'video': video, })
def get(self, request, course_id): # 此处的id为表默认为我们添加的值。 course = Course.objects.get(id=int(course_id)) # 此处是将user和course做关联; # 查询用户是否开始学习了该课程[因为已经进行了登录权限的验证,所以可以直接取值user=request.user,]; # 如果还未学习则加入用户学习的课程列表中; user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 通过添加两个类属性参数的方式来创建实例; user_course = UserCourse(user=request.user, course=course) # 课程的学习人数+1; course.students += 1 # 分别保存至数据库; course.save() user_course.save() # 通过上面所获取的课程名来查询其对应课程资源; all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系; user_courses = UserCourse.objects.filter(course=course) # 从关系中取出user_id; user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的课程,外键会自动有id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id; course_ids = [user_course.course_id for user_course in all_user_courses] # 获取学过该课程的用户学过的其他课程;‘__’双下划线表示的是一种关联关系; relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums").exclude(id=course.id)[:4] # 是否收藏课程 return render(request, "course-video.html", { "course": course, "all_resources": all_resources, "relate_courses": relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) # 查询用户是否已经学习了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 如果没有学习该门课程就关联起来 user_course = UserCourse(user=request.user, course=course) user_course.save() course.students += 1 course.save() # 相关课程推荐 # 找到学习这门课的所有用户 user_courses = UserCourse.objects.filter(course=course) # 找到学习这门课的所有用户的id user_ids = [user_course.user_id for user_course in user_courses] # 通过所有用户的id,找到所有用户学习过的所有课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [ all_user_course.course_id for all_user_course in all_user_courses ] course_ids.remove(course.id) # 通过所有课程的id,找到所有的课程,按点击量去五个 relate_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums")[:5] # 资源 all_resources = CourseResource.objects.filter(course=course) return render( request, 'course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id): course = Course.objects.get(pk=course_id) user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: course.students += 1 course.course_org.students_nums += 1 course.course_org.save() course.save() user_course = UserCourse(user=request.user, course=course) user_course.save() all_course = Course.objects.filter( tag=course.tag, course_category=course.course_category).order_by('-add_time') course_ids = [int(i.id) for i in all_course] if course.id in course_ids: course_ids.remove(int(course.id)) if course_ids is None: course_ids = [] related_courses = Course.objects.filter(id__in=course_ids)[:5] # user_ids = [user_course.user.id for user_course in user_courses] # all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # # 取出所有课程id # course_ids = [user_course.course.id for user_course in all_user_courses] # related_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] all_lesson = course.lesson_set.all() all_resource = course.courseresource_set.all() announcement = CourseAnnouncement.objects.last() return render( request, 'course/course-video.html', { 'course': course, 'all_lesson': all_lesson, 'all_resource': all_resource, 'related_courses': related_courses, 'announcement': announcement, })
def get(self, request, course_id): # 此处的id为表默认为我们添加的值。 course = Course.objects.get(id=int(course_id)) # 查询用户是否开始学习了该课,如果还未学习则,加入用户课程表,两个外键关系,user和course user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 创建 user_course = UserCourse(user=request.user, course=course) # 该课程学习人数 +1 course.students += 1 course.save() user_course.save() # 查询课程资源 all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系,即找出所有学习了该课程的 user_id:course.id user_courses = UserCourse.objects.filter(course=course) # 找出所有学习了该课程的 # 从关系中取出user_id用户id列表 user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的对应的所有课程,外键会自动有id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有学习了的课程id列表 course_id course_ids = [ user_course.course_id for user_course in all_user_courses ] # 获取学过该课程用户学过的其他课程,除了本门课程之外,取前4个 relate_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums").exclude( id=course.id)[:4] return render( request, "course-video.html", { "course": course, "all_resources": all_resources, "relate_courses": relate_courses, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) couse_it = video.lesson.course couse_it.students += 1 couse_it.save() # 查询用户是否已经学习过该课程 user_course = UserCourse.objects.filter(user=request.user, course=couse_it) if not user_course: user_course = UserCourse(user=request.user, course=couse_it) user_course.save() # 1、怎么找学过该课程的同学 user_cousers = UserCourse.objects.filter(course=couse_it) # 取到所有的学生 user_ids = [user_course.user.id for user_course in user_cousers] # 查找所有所有学生学过的课程 # filter(user_id),是因为user是UserCours的外键 # filter(user_id__in)是django model的用法 all_user_couses = UserCourse.objects.filter(user_id__in=user_ids) # 取出课程所有id coures_ids = [user_course.course.id for user_course in all_user_couses] # 取出课程 relate_couses = Course.objects.filter(id__in=coures_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course=couse_it) return render(request, "course-play.html", { "couse_it": couse_it, "all_resources": all_resources, "relate_couses": relate_couses, "video":video, })
def get(self, request, video_id): video = Video.objects.get(pk=video_id) course = video.lesson.course course.students += 1 course.save() # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() # 筛选出用户的课程 user_courses = UserCourse.objects.filter(course=course) # 取出用户的所有id user_ids = [user_course.user.id for user_course in user_courses] # 取出该用户id的所有课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [ user_course.course.id for user_course in all_user_courses ] # 获取学过该用户学过其他的所有课程 relate_courses = Course.objects.filter( id__in=course_ids).order_by('click_nums')[:5] # 课程资源 all_resources = CourseResource.objects.filter(course=course) context = { 'course': course, 'course_resources': all_resources, 'relate_courses': relate_courses, 'video': video } return render(request, 'course-play.html', context)
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) course.students += 1 course.save() user_courses = UserCourse.objects.filter(course=course) if not user_courses: usr_course = UserCourse(user=request.user, course=course) usr_course.save() user_coursers = UserCourse.objects.filter(course=course) user_id = [user_courser.user.id for user_courser in user_coursers] all_user_courses = UserCourse.objects.filter(user_id__in=user_id) course_ids = [ user_courser.course.id for user_courser in all_user_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course=course) return render( request, "course-video.html", { "course": course, "all_resources": all_resources, 'relate_courses': relate_courses, })
def get(self, request, video_id): # id是数据表里面默认为我们添加的值。 video = Video.objects.get(id=int(video_id)) # 找到对应的course course = video.lesson.course # 查询用户是否开始学习了该课 user_courses = UserCourse.objects.filter(user=request.user, course=course) # 如果还未学习则加入用户课程表 if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() # 查询课程资源 all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系 user_courses = UserCourse.objects.filter(course=course) # 从关系中取出user_id user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的课程,外键会自动有id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [ user_course.course_id for user_course in all_user_courses ] # 获取学过该课程用户学过的其他课程 related_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums").exclude( id=course.id)[:4] # 是否收藏课程 return render( request, "course-play.html", { "course": course, "all_resources": all_resources, "related_courses": related_courses, "video": video, })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) print(video.url) course = video.lesson.course course.students += 1 course.save() # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_courses = UserCourse(user=request.user, course=course) user_courses.save() all_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in all_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [ user_course.course.id for user_course in all_user_courses ] # 获取学过该课程的同学还学过的其他课程 relate_courses = Course.objects.filter(id__in=course_ids).exclude( name=course.name).order_by("-click_nums")[:5] all_resourses = CourseResource.objects.filter(course=course) return render( request, 'course-play.html', { "course": course, "all_resourses": all_resourses, "relate_courses": relate_courses, "video": video })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course # 查询用户是否关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() user_coursers = UserCourse.objects.filter(course=course) user_ids = [user_courser.user.id for user_courser in user_coursers] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所以课程id course_ids = [user_courser.course.id for user_courser in all_user_courses] # 获取学过该用户学过其他的所以课程 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5] all_resource = CourseResource.objects.filter(course=course) return render(request, 'course-play.html', { 'course': course, 'course_resources': all_resource, 'relate_courses': relate_courses, 'video': video, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) #查询用户是否已经关联了该课程 user_cou = UserCourse.objects.filter(user=request.user, course=course) if not user_cou: user_cou1 = UserCourse(user=request.user, course=course) course.student += 1 user_cou1.save() course.save() user_course = UserCourse.objects.filter(course=course) user_ids = [user_co.user.id for user_co in user_course] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [ user_course.course.id for user_course in all_user_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_name')[:3] all_resources = CourseResource.objects.filter(course=course) return render( request, 'course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id, video_id): course = Course.objects.get(id=int(course_id)) course.click_nums += 1 course.save() video = Video.objects.get(id=int(video_id)) user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() course.student += 1 course.save() user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_courses = UserCourse.objects.filter( user_id__in=user_ids).order_by('-course__click_nums')[:5] related_courses = [] for item in all_courses: if item.course.id != course.id: related_courses.append(item.course) course_resources = CourseResource.objects.filter(course=course) return render( request, 'course-play.html', { 'course': course, 'course_resources': course_resources, 'related_courses': related_courses, 'video': video, })
def get(self, request, course_id): course = models.Course.objects.get(id=int(course_id)) course.students +=1 course.save() # 查询用户是否关联此课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() user_cousers = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_cousers] all_user_courses =UserCourse.objects.filter(user_id__in=user_ids) course_ids = [user_couser.course.id for user_couser in all_user_courses] # 获取学过该用户学过其他的所有课程 relate_courses =models.Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5] all_resources = models.CourseResource.objects.filter(course=course) return render(request, "course-video.html", { "course": course, 'course_resources':all_resources, 'relate_courses':relate_courses })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course user_courses = UserCourse.objects.filter(course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() course_users = UserCourse.objects.filter(course=course) all_user_ids = [usercourse.id for usercourse in course_users] all_relate_courses = UserCourse.objects.filter( user_id__in=all_user_ids) course_ids = [ relate_course.course.id for relate_course in all_relate_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums') all_recourse = CourseResource.objects.filter(id=int(course.id)) return render( request, 'course-play.html', { 'course': course, 'all_recourse': all_recourse, 'relate_courses': relate_courses, 'video': video })
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course course.students += 1 course.save() # 查询该用户是否已经关联了该课程 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse(user=request.user, course=course) user_course.save() # 根据当前课程取出学习该课程的所有用户id user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] # 根据用户id取出他们学习的所有课程,作为相关课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有相关课程id,但不包含本课程 course_ids = [ user_course.course.id for user_course in all_user_courses if user_course.course.id != course.id ] # 获取学过该课程的用户的其他所有课程,只取点击数排前五个 relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] all_resources = CourseResource.objects.filter(course=course) return render( request, 'courses/course-play.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, 'video': video, })
def get(self, request, course_id): # 此处的id为表默认我们添加的值 course = Course.objects.get(id=int(course_id)) # 查询用户是否开始学习该课,如果还未学习则加入用户课程表 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse(user=request.user, course=course) course.students += 1 course.save() user_course.save() # 查询课程资源 all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系 user_courses = UserCourse.objects.filter(course=course) # 从关系中取出user_id user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的课程,外键会自动有id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [ user_course.course_id for user_course in all_user_courses ] # 获取学过该课程用户学过的其他课程 relate_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums").exclude( id=course.id)[:4] # 是否收藏课程 return render( request, "course-video.html", { "course": course, "all_resources": all_resources, "relate_courses": relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) all_course_resources = CourseResource.objects.filter(course=course) user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse(user=request.user, course=course) user_course.save() user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [ user_course.course.id for user_course in all_user_courses ] relate_course = Course.objects.filter( id__in=course_ids).order_by('-click_nums')[:5] current_page = 'info' return render( request, 'course-video.html', { 'course': course, 'course_resources': all_course_resources, 'current_page': current_page, 'relate_course': relate_course })
def get(self, request, course_id): #用户与课程关联 course = Course.objects.get(id=int(course_id)) user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() #课程人数+1 course.students += 1 course.save() # 机构人数+1 course.course_org.students += 1 course.course_org.save() # 此处的id为表默认为我们添加的值。 course = Course.objects.get(id=int(course_id)) course_resources = CourseResource.objects.filter(course=course).order_by('-add_time') # 找出学过这门课的同学 user_courses = UserCourse.objects.filter(course=course) user_ids = [user_course.user_id for user_course in user_courses] # 取出用户所学课程ID all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 外键filter用法 course_ids = [user_course.course_id for user_course in all_user_courses] relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums") return render(request, "course-video.html", { "course": course, "course_resources": course_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) # 查询用户是否已经关联了该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: # 若不存在创建关联 user_course = UserCourse(user=request.user, course=course) user_course.save() # 取出学过这门课程的user user_cousers = UserCourse.objects.filter(course=course) # 取出user的id user_ids = [user_couser.user.id for user_couser in user_cousers] # 取出user学得所有课程 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程id course_ids = [user_couser.course.id for user_couser in all_user_courses] # 获取学过该用户学过其他的所有课程 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:3] all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-video.html', { "course": course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id): # 根据前端传过来的ID值和数据表中自动为我们创建好的表的id值 # 获取指定课程 course = Course.objects.get(id=int(course_id)) # 查询用户是否已经学习了这门课, # 如果未开始学习这门课,则加入到用户课程表 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_courses = UserCourse(user=request.user, course=course) course.students += 1 course.save() user_courses.save() # 查询课程资源,通过课程定位课程资源 all_resources = CourseResource.objects.filter(course=course) # 选出学了这门课的学生关系 user_courses = UserCourse.objects.filter(course=course) # 列表式取出从关系中取出用户id user_ids = [user_course.user_id for user_course in user_courses] # 这些用户学了的课程,外键会自动生成id,取到字段 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 列表式取出所有课程的id course_ids = [ user_course.course_id for user_course in all_user_courses ] # 获取该课程用户学过的其他课程并排除掉登陆用户学习过的课程 relate_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums").exclude( id=course.id)[:4] return render( request, 'course-video.html', { 'course': course, 'all_resources': all_resources, 'relate_courses': relate_courses, })
def get(self, request, course_id): course = Course.objects.get(id=int(course_id)) # 查询用户是否已经关联了课程 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: user_course = UserCourse(user=request.user, course=course) user_course.save() # 学过这门课程的所有同学还学过其他的课程(相关推荐) user_courses = UserCourse.objects.filter(course=course) # python 列表式 获取所有用户对应的id if user_courses: user_ids = [user_course.user_id for user_course in user_courses] # 两个下划线加in(django model的用法) 表示user_id在后面的列表中的都返回过来,即获取所有的用户课程对应关系 all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 通过user_id获取所有课程id course_ids = [ user_course.course_id for user_course in all_user_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by('-click_num')[:5] all_comments = course.coursecomments_set.all() try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 p = Paginator(all_comments, 5, request=request) all_comments = p.page(page) return render_to_response('course-vedio.html', locals())
def get(self, request, video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course course.students += 1 course.save() # 查询是否学习该课程 user_courses = UserCourse.objects.filter(user=request.user, course=course) if not user_courses: user_course = UserCourse(user=request.user, course=course) user_course.save() # 相关课程 user_coursers = UserCourse.objects.filter(course=course) user_ids = [user_courser.user.id for user_courser in user_coursers] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) course_ids = [ all_user_course.course.id for all_user_course in all_user_courses ] relate_courses = Course.objects.filter( id__in=course_ids).order_by("-click_nums")[:5] all_resources = CourseResource.objects.filter(course=course) return render(request, "course-play.html", locals())
def get(self,request,video_id): video = Video.objects.get(id=int(video_id)) course = video.lesson.course course.students += 1 course.save() # 查询用户是否已经关联了该课程,如果未关联则将用户与该课程关联 user_couses = UserCourse.objects.filter(user=request.user, course=course) if not user_couses: user_course = UserCourse(user=request.user, course=course) user_course.save() user_courses = UserCourse.objects.filter(course=course) # 学过该课程的学员 user_ids = [user_course.user.id for user_course in user_courses] all_user_courses = UserCourse.objects.filter(user_id__in=user_ids) # 取出所有课程ID course_ids = [user_course.course.id for user_course in all_user_courses] # 获取学过该课程的用户学过的其他所有课程,取前5个 relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5] all_resources = CourseResource.objects.filter(course=course) return render(request, 'course-play.html', { "course": course, "all_resources": all_resources, "relate_courses": relate_courses, "video":video, })
def get(self, request, video_id): video = Video.objects.get(id=video_id) course = video.lesson.course course_id = course.id # 点击我要学习后,首先判断该用户是否已与该课程关联 user_course = UserCourse.objects.filter(user=request.user, course_id=course_id) if not user_course: # 若课程与用户未关联就建立关联 user_course = UserCourse(user=request.user, course_id=course_id) user_course.save() # 根据课程的id取到对应user_courses user_courses = UserCourse.objects.filter(course_id=course.id) # 根据user_courses取到学习到该课程的user_id users_id = [user_course.user_id for user_course in user_courses] # 根据学习到过该课程的user_id取到所有的相关relate_user_courses relate_user_courses = UserCourse.objects.filter(user_id__in=users_id) # 根据relate_user_courses取到相关课程的id relate_courses_id = [ relate_user_course.course_id for relate_user_course in relate_user_courses ] # 根据relate_courses_id取到相关课程 relate_courses = Course.objects.filter(id__in=relate_courses_id) # 将数据传到前端 return render(request, 'course-play.html', { 'course': course, 'relate_courses': relate_courses, 'video': video })