def post(self, request, id): name = request.POST.get('name') email = request.POST.get('email') url = request.POST.get('url') content = request.POST.get('content') c = Comment() c.name = name c.email = email c.url = url c.content = content c.article = Article.objects.get(pk=id) c.save() return JsonResponse({"name": name, "content": content, "create_time": c.create_time})
def comment_view(request): contenttype = request.POST.get('content_type') comment = Comment() if contenttype == 'essay': object_pk = request.POST.get('object_pk') content_object = Essay.objects.get(id=object_pk) comment.content_object = content_object comment.nickname = request.POST.get('name') comment.email = request.POST.get('email') comment.url = request.POST.get('url') comment.content = request.POST.get('content') comment.save() url = '/home/essay/detail/%s/' % object_pk return HttpResponseRedirect(url) return HttpResponse('error')
def post(self, request, post_id): comment = Comment() # 实例化类 comment.post_id = post_id comment.parent_id = request.POST['parent_id'] comment.reply_id = request.POST['reply_id'] comment.nick = request.POST['nick'] comment.mail = request.POST['mail'] comment.content = request.POST['content'] ua = parse_user_agent(request.META.get('HTTP_USER_AGENT', '')) # 解析HTTP_USER_AGENT comment.browser = ua['browser'] comment.client = ua['client'] # 处理回复评论 if request.POST['reply_id'] != '0': comment.comment_type = 'reply' reply_comment = Comment.objects.filter( id=request.POST['reply_id']).first() if reply_comment: comment.to_nick = reply_comment.nick comment.to_mail = reply_comment.mail # 如果是回复评论,则发送邮件通知相关评论人 recipient_list = EMAIL_RECEIVE_LIST + [reply_comment.mail] else: recipient_list = None else: # 如果是新的评论内容,则只需要发送通知博客作者 recipient_list = EMAIL_RECEIVE_LIST comment.save() # 保存评论数据到数据库 redirect_url = request.POST['redirect_url'] + '#comment-{0}'.format( comment.id) if recipient_list: # 发送邮件 try: send_email(url=redirect_url, recipient_list=recipient_list, post_id=post_id) except BaseException as e: print('发送邮件错误: {}'.format(e)) return redirect(redirect_url) # 重定向到指定页面
def post(self, request, *args, **kwargs): target_id = request.POST.get('post_id') target = Post.objects.filter(id=target_id)[0] content = request.POST.get('content') # content=mistune.markdown(raw_content) nickname = request.user.username comment = Comment() success = True msg = '' if nickname: comment.nickname = nickname else: msg = "必须登陆才能评论!" success = False if success: comment.content = content comment.target = target comment.save() return JsonResponse(data={'success': success, 'msg': msg})