def post(self, request): root_id = int(request.POST.get('root_comment_id', 0)) #顶级评论的id reply_id = int(request.POST.get('reply_comment_id', 0)) #上一级回复的id reply_user_id = int(request.POST.get('reply_user_id', 0)) #上一级回复user的id course_id = request.POST.get('course_id', 0) # 课程id root_comment_text = request.POST.get('comment', '') #回复顶级评论的内容 ptn_text = request.POST.get('ptn', '') #回复回复评论的内容 try: root_id = CourseComments.objects.get(pk=root_id) #获取"一个"评论对象(顶级评论) reply_id = CourseComments.objects.get(pk=reply_id) reply_user_id = UserProfile.objects.get(pk=reply_user_id) #回复给谁 except: return Fail('回复失败') #回复内容传递给数据库 course_comment = CourseComments() course_comment.user = request.user #回复的作者 course_comment.course = Course.objects.get(pk=course_id) course_comment.root = root_id course_comment.parent = reply_id course_comment.reply_to = reply_user_id if ptn_text == '': #如果回复评论的内容为空,则把回复顶级内容赋值给评论内容 if root_comment_text == '': #如果回复顶级内容为空,抛出错误 return Fail('回复不能为空') course_comment.comment = root_comment_text else: if ptn_text == '': #如果回复回复评论内容为空,抛出错误 return Fail('回复不能为空') course_comment.comment = ptn_text course_comment.save() return Success('评论成功') #坑,保存数据库后,记得返回ajax成功信息使得刷新页面
def post(self, request): if request.user.is_authenticated: course_id = request.POST.get('course_id', 0) comment = request.POST.get('comments', '') if int(course_id) > 0 and comment: course_comment = CourseComments() course = Course.objects.get(id=int(course_id)) course_comment.course = course course_comment.user = request.user course_comment.comment = comment course_comment.save() return HttpResponse("{'status': 'success', 'msg':'添加成功'}", content_type='application/json') return HttpResponse("{'status': 'fail', 'msg':'添加失败'}", content_type='application/json') return HttpResponse("{'status': 'fail', 'msg':'未登录'}", content_type='application/json')
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) comment = request.POST.get("comments", "") if int(course_id) > 0 and comment: course_comment = CourseComments() course = Course.objects.get(id=int(course_id)) course_comment.course = course course_comment.comment = comment course_comment.user = request.user course_comment.save() return HttpResponse('{"status":"success", "msg":"添加成功"}', content_type='application/json') else: return HttpResponse('{"status":"success", "msg":"添加失败"}', content_type='application/json')
def post(self, request, course_id): if not request.user.is_authenticated: return HttpResponse('{"status":"failed", "msg":"用户未登陆"}', content_type="application/json") comment = request.POST.get("comment", '') if Course.objects.filter(id=course_id).exists() and comment: new_comment = CourseComments() new_comment.user = request.user new_comment.comment = comment new_comment.course_id = int(course_id) new_comment.save() return HttpResponse('{"status":"successed"}', content_type="application/json") else: return HttpResponse('{"status":"failed", "msg":"提交了错误的数据"}', content_type="application/json")
def post(self, request): obj_id = request.POST.get('course_id', 0) #对象pk comments = request.POST.get('comments', '') #评论内容 if not request.user.is_authenticated: return Fail('用户未登录') #实例化 course_comments = CourseComments() #如果获取有效的 if int(obj_id) > 0 and comments != '': course_comments.user = request.user course_comments.course = Course.objects.get(pk=int(obj_id)) #评论的对象 course_comments.comment = comments course_comments.save() return Success('评论成功') else: return Fail('评论失败')
def post(self, request): comments = request.POST.get('comments', '') course_id = request.POST.get('course_id', '') if Course.objects.filter(id=int(course_id)): course = Course.objects.get(id=int(course_id)) course_comment = CourseComments() course_comment.user = request.user course_comment.course = course course_comment.comment = comments course_comment.save() return HttpResponse(json.dumps({ 'status': 'success', 'msg': '评论提交成功' }), content_type='application/json') else: return render(request, 'course_not_exist.html')
def post(self, request): course_id = request.POST.get('course_id', 0) comment = request.POST.get('comments', '') if not (course_id > 0 and comment): return JsonResponse({'status': 'failed', 'msg': u'失败'}) user = request.user if not user.is_authenticated(): return JsonResponse({'status': 'failed', 'msg': '用户未登录'}) course_comment = CourseComments() course_comment.user = user course_comment.course = Course.objects.get(id=course_id) course_comment.comment = comment course_comment.save() result = {'status': 'success', 'msg': u'成功'} return JsonResponse(result)
def post(self, request): course_id = request.POST.get('course_id', '') content = request.POST.get('comments', '') course = Course.objects.get(id=int(course_id)) if not request.user.is_authenticated(): return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') if Course.objects.get(id=int(course_id)): course = Course.objects.get(id=int(course_id)) comment = CourseComments() comment.course = course comment.comment = content comment.user = request.user comment.save() return HttpResponse('{"status":"success", "msg":"添加成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"course_id erro"}', content_type='application/json')
def post(self, request): if not request.user.is_authenticated(): return HttpResponse('{"status":"success", "msg": "用户未登录"}', content_type='application/json') course_id = request.POST.get('course_id', 0) comments = request.POST.get('comments', '') if course_id > 0 and comments: course = Course.objects.get(id=int(course_id)) course_comments = CourseComments() course_comments.user = request.user course_comments.comment = comments course_comments.course = course course_comments.save() return HttpResponse('{"status":"success", "msg": "评论成功"}', content_type='application/json') else: return HttpResponse('{"status":"success", "msg": "评论失败"}', content_type='application/json')
def post(self,request): if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 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: # 判断传回来的值是否有效 id>0 且 评论不能为空 course_comments = CourseComments() course = Course.objects.get(id = int(course_id)) # get只能取出一条数据,如果有多条抛出异常。没有数据也抛异常 # filter取一个列表出来,queryset。没有数据返回空的queryset不会抛异常 course_comments.course = course course_comments.user = request.user course_comments.comment = comments course_comments.save() return HttpResponse('{"status":"success", "msg":"评论成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"评论失败"}', content_type='application/json')
def post(self, request): if not request.user.is_authenticated(): # 判断用户登陆状态,未登录则放回json,并转跳登陆页面 return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') demo_id = request.POST.get("course_id", 0) comments = request.POST.get("comments", "") if demo_id > 0 and comments: course_comments = CourseComments() demo = Demo.objects.get(id=int(demo_id)) course_comments.demo = demo course_comments.comment = 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')