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 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 addcomment(request, id): url = request.META.get('HTTP_REFERER') if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): current_user = request.user data = Comment() data.user_id = current_user.id data.post_id = id data.rate = form.cleaned_data['rate'] data.subject = form.cleaned_data['subject'] data.comment = form.cleaned_data['comment'] data.ip = request.META.get('REMOTE_ADDR') data.save() messages.success(request, "Your comment succesfully send. Thank you") return HttpResponseRedirect(url) messages.warning(request, "Vups, Someting went wrong. Chechk again") return HttpResponseRedirect(url)
def post(request, post_id): if request.method == "GET": p = get_object_or_404(Post, pk=post_id) return render_to_response('post/templates/post.html', {'post': p, 'user': request.user}, context_instance=RequestContext(request)) else: print request.POST comment = request.POST.get("comment") if comment: c = Comment() c.post_id = post_id c.comment = comment c.username = request.user.username c.rating = 0 c.save() p = get_object_or_404(Post, pk=post_id) return render_to_response('post/templates/post.html', {'post': p, 'user': request.user}, context_instance=RequestContext(request)) else: return HttpResponse("<strong>you must provide valid comment</strong>")