def post(self, request): comment_form = CommentForm(request.POST, user=request.user) data = {} if comment_form.is_valid(): # 检查通过,保存数据 comment = Comment() comment.content = comment_form.cleaned_data['content'] comment.article = comment_form.cleaned_data['content_object'] comment.user = comment_form.cleaned_data['user'] # 判断是否是回复 parent = comment_form.cleaned_data['parent'] if parent: comment.root = parent.root if parent.root else parent comment.parent = parent comment.reply_to = parent.user comment.save() # 发送邮件 comment.sen_email(request) # 返回评论数据.get_nickname()是users:model里面的如果有昵称返回昵称,没有就返回手机号,是评论不刷新页面出现 data['status'] = 'SUCCESS' data['user'] = comment.user.get_nickname() data['created'] = comment.created.strftime('%Y-%m-%d %H:%M:%S') data['content'] = comment.content data['content_type'] = comment.article._meta.model_name # return JsonResponse(data) # 返回回复数据 if parent: data['reply_to'] = comment.reply_to.get_nickname() else: data['reply_to'] = '' data['id'] = comment.id data['root_id'] = comment.root.id if comment.root else '' # print("#root_%s" % data['root_id']) else: data['status'] = 'ERROR' data['message'] = list(comment_form.errors.values())[0][0] return JsonResponse(data)
def ArticleDetailView(request, article_id): ''' 文章内容 ''' if request.method == "GET": post = get_object_or_404(Article, id=article_id) # + comments = Comment.objects.filter( article=post).order_by('-created_time') md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) post.body = md.convert(post.body) post.toc = md.toc return render(request, 'blog/detail.html', context={ 'post': post, 'comments': comments }) if request.method == "POST": article_id = request.POST.get('id') post = get_object_or_404(Article, id=article_id) comment = Comment() text = request.POST.get("text", "") comment.article = post comment.content = text comment.save() #path=reverse('home:detail',args=(article_id))+'#comment_dot' return redirect( request.META['HTTP_REFERER']) #HttpResponseRedirect(path)