def post_detail(request, pk): post = Post.objects.get(pk=pk) post.view += 1 post.save() if request.method == 'POST': form = CommentForm(request.POST or None) if form.is_valid(): form.instance.user = request.user form.instance.post = post form.save() messages.success( request, 'Comment added...Your comment will appear after proper verification.. Thnaks..' ) # redirect('post-detail',kwargs={'pk':post.pk}) else: pass context = { 'object': post, 'form': CommentForm(), 'navs': Category.objects.all(), 'cats': Category.objects.all().order_by('-id'), 'popular_posts': Post.objects.filter(view__gte=4).order_by('-id')[:6] } return render(request, 'blog/post_detail.html', context)
def add(request): path = request.path if request.method == "POST": form = CommentForm(request.POST) print form if form.is_valid(): Comment.objects.create( user=request.user, album=Album.objects.get(pk=request.POST["album"]), title=form.cleaned_data["title"], content=form.cleaned_data["content"], ) return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
def comment(request, reviewid): if request.method == "POST": comment = Comment(user=request.user, review=Review.objects.get(pk=reviewid)) form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() isbn = Review.objects.get(pk=reviewid).book.isbn return HttpResponseRedirect('/book/'+str(isbn)) else: form = CommentForm() return render_to_response('comment.html', {'form': form, 'review': Review.objects.get(pk=reviewid), }, context_instance=RequestContext(request) )
def add_comment_to_post(request, pk): form = CommentForm(request.POST) post = Post.objects.filter(pk=pk).first() if request.method == "POST": if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post comment.save() else: form = CommentForm() context = { 'author': comment.author.username, 'created_date': comment.created_date, 'text': comment.text, 'post_id': comment.post.id } return JsonResponse(context, safe=False)