22、Django实战第22天:课程评论
1、编辑course-comment.html
2、编辑courses.views.py
class CourseCommentView(LoginRequiredMixin, View): def get(self, request, course_id): course = Course.objects.get(id=course_id) return render(request, 'course-comment.html', { 'course': course, })
3、配置url
... from .views import CourseCommentView urlpatterns = [ ... url(r'comment/(?P<course_id>\d+)/$', CourseCommentView.as_view(), name='course_comment'), ]
4、在章节前端页面(course-video.html)修改链接
5、在评论前端页面(course-comment.html)修改链接
7、资料下载、讲师提示、课程推荐和之前一样
class CourseCommentView(LoginRequiredMixin, View): def get(self, request, course_id): course = Course.objects.get(id=course_id) all_resource = CourseResource.objects.filter(course=course) # 查询用户是否已经关联了该数据 user_course = UserCourse.objects.filter(user=request.user, course=course) if not user_course: # 如果没有则写入数据库 my_course = UserCourse(user=request.user, course=course) my_course.save() # 该同学还学过 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) #获取这些用户学过的课程记录 course_ids = [user_course.id for user_course in all_user_courses] #获取这些课程的id relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5] #根据点击量取出5个 return render(request, 'course-comment.html', { 'course': course, 'all_resource': all_resource, 'relate_courses': relate_courses, })
8、上面的前端部分这里就不截图演示了
评论部分
编辑courses.view.py,创建一个评论的类
... from operation.models import CourseComments from django.http import HttpResponse class AddCommentView(View): def post(self, request): if not request.user.is_authenticated(): # 判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') course_id = request.POST.get('course_id', 0) comments = request.POST.get('comments', '') if int(course_id) > 0 and comments: course_comments = CourseComments() course = Course.objects.get(id=int(course_id)) course_comments.course = course course_comments.comments = comments course_comments.user = request.user course_comments.save() return HttpResponse('{"status":"success", "msg":"添加成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"添加失败"}', content_type='application/json')
配置url,编辑courses.urls.py
... from .views import AddCommentView urlpatterns = [ ... url(r'add_comment/$', AddCommentView.as_view(), name='add_comment'), ]
编辑course-comment.html中的js部分
编辑courses.models.py,利用外键获取本课程的评论
class Course(models.Model): name = models.CharField(max_length=50, verbose_name='课程名') course_org = models.ForeignKey(CourseOrg, verbose_name='课程机构') desc = models.CharField(max_length=300, verbose_name='课程描述') details = models.TextField(verbose_name='课程详情') degree = models.CharField(verbose_name='课程难度', 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='点击数') category = models.CharField(max_length=10, verbose_name=u'类别', default='') tag = models.CharField(max_length=10, verbose_name=u'标签', default='') teacher = models.ForeignKey(Teacher, verbose_name='课程讲师', null=True, blank=True) youneed_know = models.CharField(default='', max_length=300, verbose_name='课程须知') teacher_tell = models.CharField(default='', max_length=300, verbose_name='你能学到') add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间') class Meta: verbose_name = '课程' verbose_name_plural = verbose_name def get_lesson_nums(self): """获取章节数""" return self.lesson_set.count() def get_lesson(self): """获取章节""" return self.lesson_set.all() def get_students(self): """获取3个学习用户""" return self.usercourse_set.all()[:3] def get_comment(self): """获取评论""" return self.coursecomments_set.all() def __str__(self): return self.name
前端页面动态展示评论
每天进步一点,加油!