def post(self, request): data = json.loads(request.body.decode('utf-8')) try: if data['content_input']: new_cm = Comment() new_cm.content = data['content_input'] new_cm.user_id = request.user.id new_cm.post_id = data['post_id'] new_cm.save() return HttpResponse( 'bình luận thành công, hãy tiếp tục tương tác nhé') except: pass z = Comment.objects.filter( post=data['post_id']).order_by('-created_at') comments = [] for i in z: d = model_to_dict(i.user) del d['password'], d['cover_image'], d['avatar'], d['email'], d[ 'date_joined'] out = { **model_to_dict(i), **d, "created_at": i.created_at.strftime("%H:%M:%S ngày %m/%d/%Y") } comments.append(out) return JsonResponse(comments, safe=False)
def post_detail(request, id_): post = Post.objects.get(id=id_) rel_post = Post.objects.order_by('-date_posted').filter( tag=post.tag, for_cooperative__exact=False).all() if request.method == 'POST': content = str(request.POST.get('content', False)) if content: comment = Comment() comment.author_id = request.user.id comment.author_status = author_status(request.user.id) comment.date_posted = timezone.now() comment.content = content comment.post = post comment.save() return redirect('/post/' + str(id_) + '/') else: return render(request, 'post/post_detail.html', { 'post': post, 'related': rel_post }) return render(request, 'post/post_detail.html', { 'post': post, 'related': rel_post })
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 add_comment(request, article_id): form = CommentForm(request.POST) post = get_object_or_404(Post, id=article_id) if form.is_valid(): comment = Comment() comment.path = [] comment.post_id = post comment.author_id = auth.get_user(request) comment.content = form.cleaned_data['comment_area'] comment.save() return redirect(post.get_absolute_url())
def post(self, request): data = json.loads(request.body.decode('utf-8')) try: if data['content_input']: new_cm = Comment() new_cm.content = data['content_input'] new_cm.user_id = request.user.id new_cm.post_id = data['post_id'] new_cm.save() return HttpResponse( 'bình luận thành công, hãy tiếp tục tương tác nhé') except: pass database = Database(request.user.id) get_comment_post_id = database.get_comment_post_id(data['post_id']) return JsonResponse({'result': get_comment_post_id})
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)
def add_Comment(request, article_id): form = CommentForm(request.POST) article = get_object_or_404(Article, id=article_id) if form.is_valid(): comment = Comment() comment.path = [] comment.article_id = article comment.author_id = auth.get_user(request) comment.content = form.cleaned_data['comment_area'] comment.save() 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(article.get_absolute_url())