东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3111|回复: 1
打印 上一主题 下一主题

[课堂笔记] 45、python_django_web开发_课程评论页面的开发_笔记

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14432
QQ
跳转到指定楼层
楼主
发表于 2017-12-4 08:29:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


45、python_django_web开发_课程评论页面的开发_笔记
发表评论功能 Ajax向后台发送

  1. # 某个课程的评论
  2. url(r'^comment/(?P<course_id>[0-9]+)/

  3. , CourseCommentView.as_view(), name='course_comment'),

  4. # 添加用户对课程的评论
  5. url(r'^add_comment/

  6. , AddCommentView.as_view(), name='add_course_comment'),

  7. class CourseCommentView(View):
  8.     def get(self, request, course_id):
  9.         course = Course.objects.get(id=int(course_id))
  10.         # all_course_resource = CourseResource.objects.filter(course_id=int(course_id))
  11.         all_course_resource = course.courseresource_set.all()

  12.         all_comments = course.coursecomment_set.all().order_by('-add_time')

  13.         return render(request, 'course-comment.html', {
  14.             'course': course,
  15.             'all_course_resource': all_course_resource,
  16.             'all_comments': all_comments
  17.         })

  18. class AddCommentView(View):
  19.     def post(self, request):
  20.         # 判断用户是否登录
  21.         if not request.user.is_authenticated():
  22.             return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json')

  23.         course_id = request.POST.get('course_id', '0')
  24.         comment_content = request.POST.get('comment', '')
  25.         if course_id > 0 and comment_content:
  26.             course_comment = CourseComment()
  27.             course = Course.objects.get(id=int(course_id))
  28.             course_comment.comments = comment_content
  29.             course_comment.course = course
  30.             course_comment.user = request.user
  31.             course_comment.save()
  32.             return HttpResponse('{"status":"success", "msg":"评论成功"}', content_type='application/json')
  33.         else:
  34.             return HttpResponse('{"status":"fail", "msg":"评论出错"}', content_type='application/json')



复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

132

帖子

278

积分

人工智能VIP

Rank: 9Rank: 9Rank: 9

积分
278
沙发
发表于 2017-12-14 14:52:22 | 只看该作者
1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-26 00:28 , Processed in 0.169299 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表