def add_comment_to_post(request, pk): # either get post object with corresponding pk or show 404 page post = get_object_or_404(Post, pk=pk) #if someone filled out the form and hit the submit button (method == 'POST') if request.method == 'POST': #pass the request to the Comment form and make it form form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) # post object is in the comment form as a foreign key from Post model # So here we say because the form is valid make the comment.post equal to the post itself comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: #if not hit submit yet show the CommentForm (connected in the comment_form.html) form = CommentForm() return render(request, 'blog/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) comment.post = post comment.save() notification = Notification() notification.person = comment.author notification.action = 'đã thêm một bình luận ở bài viết' notification.post_title = '\"' + post.title + '\"' notification.target = 'post' notification.id_target = post.pk notification.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def add_comment_to_post(request, pk): # getting the post post = get_object_or_404(Post, pk=pk) # if it is a post object if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) # setting the relavent post for the comment using COmments models foriegn key comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: # if it is a get request form = CommentForm() #rendering objets got by GET or POST requests return render(request, template_name='blog/comment_form.html', context={'form': form})
def blogPostDetails(request, pk): '''Method for rendering the page of blog Posts in detail ''' post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data['author'], body=form.cleaned_data['body'], post=post) comment.save() return redirect('blogPostDetails', pk=pk) comments = Comment.objects.filter(post=post) context = { 'comments': comments, 'post': post, 'form': form, } return render(request, "blogPostDetails.html", context)
def get_details(request, blog_id): blog = get_object_or_404(Blog, id=blog_id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data cleaned_data['blog'] = blog Comment.objects.create(**cleaned_data) else: form = CommentForm() content = { 'blog': blog, 'comments': blog.comment_set.all().order_by('-created'), 'form': form } return render(request, 'blogs/detail.html', content)
def addcomment(request, article_id): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.recipe_id = article_id form.save() return HttpResponseRedirect('/post/%s/' % article_id)
def get_details(request, blog_id): try: blog = Blog.objects.get(id=blog_id) except Blog.DoesNotExist: raise Http404 if request.method == 'GET': form = CommentForm() else: form = CommentForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data cleaned_data['blog'] = blog Comment.objects.create(**cleaned_data) ctx = { 'blog': blog, 'comments': blog.comment_set.all().order_by('-created'), 'form': form } return render(request, 'blog_details.html', ctx)
def add_comment_to_post(request, pk): post = get_object_or_404( Post, pk=pk) #gets post of primary key 'pk' object or gives a 404 error if request.method == 'POST': form = CommentForm(request.POST) #grabs the data posted if form.is_valid(): comment = form.save( commit=False ) #creates a form called comment but does not commit data comment.post = post #Comment model has an attribute called post that is a foreign key. This makes that foreign key equal to the post comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def add_comment_to_post(request, pk): form = CommentForm() post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['comment'] comment = Comment() comment.comment = text comment.post = post comment.author = request.user comment.save() return redirect('post_detail', pk=pk) else: CommentForm() return render(request, 'blog/comment_add.html', {'form': form})
def edit_comment(request, slug, pk): post = Post.objects.get(slug=slug) comment = Comment.objects.get(pk=pk) if comment.user != request.user: raise Http404 if request.method == 'POST': form = CommentForm(data=request.POST, instance=comment) if form.is_valid(): form.save() message = f"Your comment has been edited!" messages.add_message(request, messages.SUCCESS, message) return redirect('post_detail', slug) else: form = CommentForm(instance=comment) return render(request, 'posts/edit_comment.html', { 'comment': comment, 'form': form, })
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data['author'], body=form.cleaned_data['body'], post=post) comment.save() comments = Comment.objects.filter(post=post) context = { 'post': post, 'comments': comments, 'form': form, } return render(request, 'blog_detail.html', context)
def create_comment(request): blog_post = Article.objects.get(pk=request.POST['blog_post_id']) form = CommentForm(request.POST) if form.is_valid(): comment = form.instance comment.blog_post = blog_post comment.save() return HttpResponseRedirect('/post/' + request.POST['blog_post_id']) else: return render(request, 'blog_post.html', {'form': form})
def blog(request, blog_slug): blog = Blog.objects.get(slug=blog_slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.blog = blog comment.save() form = CommentForm() else: form = CommentForm() context = { 'categories': Category.objects.order_by('title'), 'blog': Blog.objects.get(slug=blog_slug), 'form': form } return render(request, 'blog.html', context)
def post_detail_view(request,year,month,day,post): post=get_object_or_404(Post,slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) comments = post.comments.filter(active=True) csubmit = False if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() else: form = CommentForm() return render(request,'blog/post_detail.html',{'post':post,'form':form,'csubmit':csubmit,'comments':comments})
def post(self, request, *args, **kwargs): if not request.user.is_authenticated: return HttpResponseRedirect(reverse('login')) else: comment_form = CommentForm(request.POST) if comment_form.is_valid(): PostComment.objects.create(comment_text=comment_form.cleaned_data['comment_text'], post=super().get_object(), author=request.user) return super().get(self, request, *args, **kwargs)
def test_comment_form_with_valid_data(self): """ Test comment form with valid data """ form = CommentForm({ 'name': 'John Doe', 'email': '*****@*****.**', 'body': 'Some Comment' }) self.assertTrue(form.is_valid())
def add_comment_to_post(request,pk): ''' Adding a comment to the post ''' 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,'blog/comment_form.html',{'form':form})
def article_detail(request,pk): article = Article.objects.get(pk=pk) comments = Comment.objects.filter(article__pk=pk) return render( request,'article_detail.html', { 'article':article, 'comments':comments, 'commentform':CommentForm({'article':pk}) } )
def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data() post = context['post'] context['form'] = CommentForm() context['comments'] = post.comments.filter(is_confirmed=True) context['settings'] = post.post_setting context['category'] = post.category context['author'] = 'post.author' context['categories'] = Category.objects.all() return context
def detail_blog_view(request, slug): context = {} blog_post = get_object_or_404(BlogPost, slug=slug) form = CommentForm() context['blog_post'] = blog_post context['form'] = form return render(request, 'blog/detail_blog.html', context)
def add_comment_to_post(request, pk): current_user = request.user currentUser = current_user.first_name print(currentUser) 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.author = currentUser comment.approve() comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/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(): # Save the form comment = form.save(commit=False) # Model Comment has an attribute 'post' which is a ForeignKey # To the actual Post. This means literally make comment.post # equal to Post comment.post = post # Save all the information comment.save() return redirect('post_detail', pk=post.pk) else: # Create the form and just open the comment_form.html section # with the form to be filled up again form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() comments = Comment.objects.filter(post=post) context = { "post": post, "comments": comments, "form": form, } return render(request, "blog_detail.html", context)
def post(request, post_id): post = get_object_or_404(Post, pk=post_id) comments = post.comment_set.all() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = request.user comment.date_published = timezone.now() comment.save() return HttpResponseRedirect( reverse('blog:post', args=(comment.post.id, ))) else: form = CommentForm() context = {'post': post, 'comments': comments, 'form': form} return render(request, 'blog/post_detail.html', context)
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() context = { "post": post, "form": form, } return render(request, "blog_detail.html", context)
def post(self, request, *args, **kwargs): form = CommentForm(request.POST or None) if form.is_valid(): post = get_object_or_404(Post, pk=kwargs['pk']) reply_id = request.POST.get('comment_id') comment_parent = None if reply_id: comment_parent = Comment.objects.get(id=reply_id) post.comment_set.create(Name=form.cleaned_data['Name'], email=form.cleaned_data['email'], body=form.cleaned_data['body'], reply=comment_parent) messages.success(request, 'Comment successful') images = Images.objects.filter(post=post) form = CommentForm() comments = Comment.objects.filter(post=post, reply=None).order_by('-id') context = { 'post': post, 'images': images, 'form': form, 'comments': comments } return render(request, 'blog/post_detail.html', context) form = CommentForm() post = get_object_or_404(Post, pk=kwargs['pk']) messages.warning(request, 'Some error occured') comments = Comment.objects.filter(post=post, reply=None).order_by('-id') images = Images.objects.filter(post=post) context = { 'post': post, 'images': images, 'form': form, 'comments': comments } return render(request, 'blog/post_detail.html', context)
def post_detail(request, pk): post = get_object_or_404(Blog, pk=pk) comments = comment.objects.filter(post=post).order_by('-id')[:5] comment_full = comment.objects.filter(post=post).order_by('-id') comment_count = comment.objects.filter(post=post) comment_actual_count = comment_count.count() there_are_comments = False if comment_actual_count > 0: there_are_comments = True likes = post.likes.count() dislikes = post.dislikes.count() score = likes - dislikes if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): content = request.POST.get('content') new_comment = comment.objects.create(post=post, user=request.user, content=content) new_comment.save() return HttpResponseRedirect(Blog.redirect_route(blog_id=pk)) else: comment_form = CommentForm() is_commented = False if comment.objects.filter(user=request.user.id, post=post).exists(): is_commented = True context = { 'score': score, 'there_are_comments': there_are_comments, 'post': post, 'is_liked': post.likes.filter(id=request.user.id).exists(), 'is_disliked': post.dislikes.filter(id=request.user.id).exists(), 'likes': post.likes, 'dislikes': post.dislikes, 'comments': comments, 'comment_form': comment_form, 'comment_full': comment_full, 'is_commented': is_commented } return render(request, 'blog/post_detail.html', context)
def view_post(request, pk): template_name = 'blog/post_detail.html' post = get_object_or_404(Post, pk=pk) form = CommentForm(request.POST or None) if form.is_valid(): form.instance.post = post form.instance.commented_by = request.user form.save() return redirect('view_post', pk=pk) comments = Comment.objects.filter(post=post) return render(request, template_name, {'post': post, 'form': form, 'comments': comments})
def create_comment(request): article = Article.objects.get(pk=request.POST['post_id']) form = CommentForm(request.POST) path = '/posts/' + str(article.pk) if form.is_valid(): new_comment = form.save(commit=False) new_comment.article = article new_comment.save() return HttpResponseRedirect(path) else: print(form.errors)
def post(self, request): a=request.POST.get('post') print a if request.GET: print "get" if request.POST: print "post" create_comment = CommentForm(request.POST) if create_comment.is_valid(): create_comment.save() return HttpResponseRedirect("/blog/"+str(a)+"/mainblog2/")