def add_comment(request, pk): form = CommentForm(request.POST) post = get_object_or_404(Post, id=pk) if form.is_valid(): comment = Comment() comment.path = [] comment.comment_post = post comment.author = auth.get_user(request) comment.content = form.cleaned_data['comment_area'] comment.save() # Django не позволяет увидеть ID комментария по мы не сохраним его, # сформируем path после первого сохранения # и пересохраним комментарий try: comment.path.extend( Comment.objects.get( id=form.cleaned_data['parent_comment']).path) comment.path.append(comment.id) except ObjectDoesNotExist: comment.path.append(comment.id) comment.save() return redirect(comment.get_absolute_url())
def comment(request): user = get_user(request) article_id = request.POST.get('article_id') comment = Comment() comment.article_id = article_id comment.author = user.username comment.content = request.POST.get('content') comment.save() article = Article.objects.filter(id=article_id).first() article.comment_count += 1 article.save() message = '评论成功' add_message(request, messages.INFO, message) current_path = request.POST.get('current_path') return HttpResponseRedirect(current_path)