def edit_comment(request, post_id, comment_id): """Edit an existing comment.""" post = get_object_or_404(Post, id=post_id) comment = get_object_or_404(Comment, id=comment_id) # Make sure the post belongs to the current user. if comment.owner != request.user: return HttpResponseForbidden('<h1>403 Forbidden</h1>') # Make sure the comment belongs to the current post. if post.id != comment.comment_post.id: raise Http404 if request.method != 'POST': # Initial request; pre-fill form with the current comment. form = CommentForm(instance=comment) else: # POST data submitted; process data. form = CommentForm(instance=comment, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('post', args=[post.id])) context = {'comment': comment, 'form': form} return render(request, 'blogs/edit_comment.html', context)
def create_comment(request, post_id, slug): filled_form = CommentForm(request.POST) if filled_form.is_valid(): temp_form = filled_form.save(commit=False) temp_form.post = Post.objects.get(id=post_id) filled_form.save() return redirect('blogs:post_detail', slug)
def comment(request, blogID): if request.method == 'POST': form = CommentForm(request.POST) # check whether it's valid: if form.is_valid(): blog = Blog.objects.get(num=blogID) user = blog.writer token = request.META.__getitem__('HTTP_X_TOKEN') if default_token_generator.check_token(user, token): x = form.save(commit=False) x.post = Post.objects.get(num=request.POST.get('post_id')) x.save() return JsonResponse({ 'status': 0, 'post_id': request.POST.get('post_id') }) else: return JsonResponse({ 'status': -1, 'message': 'you are not logged in' }) else: return JsonResponse({ 'status': -1, 'message': list(form.errors.keys())[0] + ": " + form.errors[list(form.errors.keys())[0]][0] })
def post(self, request, slug): if request.user.is_authenticated: comment = request.POST.get('comment') blog = Blog.objects.filter(is_active=True, slug=slug).first() comment_json = { 'content': comment, 'blog': blog, 'user': request.user } form = CommentForm(comment_json) if form.is_valid() and request.recaptcha_is_valid: form.save() messages.success(request, 'Comment added successfully.', extra_tags='comment') return redirect(reverse('blogs:blog-detail', args=[slug])) return redirect('{}?next={}'.format( reverse('login'), reverse('blogs:blog-detail', args=[slug])))
def showPost(request, postid): post = Post.objects.get(pk=postid) comments = post.comment_set.all() comment_form = CommentForm() tags = post.tags.all() context = { 'post': post, 'comments': comments, 'comment_form': comment_form, 'tags': tags } if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = request.user comment.name = request.user comment.post = post comment_form.save() return render(request, 'blogs/show.html', context)
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm(request.Post) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blogs/comment_form.html', {'form': form})
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = CommentForm(request.POST) form.instance.created_by = request.user if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('blogs:post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blogs/comment_form.html', {'form': form})
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) #Here Model CommentPost has ForeignKey Known as Post. SO that Comment can be added to that particular post. comment.post = post comment.save() redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blogs/comment_form.html', {'form': form})
def post_detail(request, post_id): post = get_object_or_404(Post, id=post_id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): data = form.save(commit=False) data.post = post data.save() else: form = CommentForm() context = {'post': post, 'form': form} return render(request, 'blogs/post_detail.html', context=context)
def new_comment(request, post_id): """Add a new comment.""" post = get_object_or_404(Post, id=post_id) if request.method != 'POST': # No data submitted; create a blank form. form = CommentForm() else: # POST data submitted; process data. form = CommentForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.owner = request.user new_comment.comment_post = post new_comment.save() return HttpResponseRedirect(reverse('post', args=[post.id])) context = {'form': form} return render(request, 'blogs/new_comment.html', context)