22、Django實戰第22天:課程評論
阿新 • • 發佈:2018-03-25
for 下載 png count() cli category str dcom ima
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
前端頁面動態展示評論
22、Django實戰第22天:課程評論