def add_comment(request, article_id): if request.POST and ('pause' not in request.session): form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comments_article = Article.objects.get(id=article_id) form.save() request.session.set_expiry(30) request.session['pause'] = True return redirect('/articles/%s/' % article_id)
def blog_details(request, slug): blog = Blog.objects.get(slug=slug) comment_form = CommentForm() already_liked = Likes.objects.filter(blog=blog, user=request.user) if already_liked: liked = True else: liked = False 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.blog = blog comment.save() return HttpResponseRedirect( reverse('Blog:blog_details', kwargs={'slug': slug})) return render(request, 'Blog/blog_details.html', context={ 'blog': blog, 'comment_form': comment_form, 'liked': liked })
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.comment.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() csubmit = True else: form = CommentForm() return render(request, 'Blog/post_detail.html', { 'post': post, 'form': form, 'comments': comments, 'csumit': csubmit })
def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) new_comment = None # List of active comments for this post comments = post.comments.filter(active=True) if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # create comment object from comment_form object with out saving to db. new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() else: comment_form = CommentForm() return render( request, 'blog/post/detail.html', { 'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form })
def add_comment_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, '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.author = request.user comment.save() return redirect('Blog:post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'Blog/comment_form.html', {'form': form})
def blogpost_detail(request, slug): post = get_object_or_404(BlogPost, slug=slug) comments = post.comments_on_blog.filter(active=True, parent__isnull=True) is_liked = False if post.likes.filter(id=request.user.id).exists(): is_liked = True if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): parent_obj = None try: parent_id = int(request.POST.get('parent_id')) except: parent_id = None if parent_id: parent_obj = Comment.objects.get(id=parent_id) if parent_obj: replay_comment = comment_form.save(commit=False) replay_comment_name = request.user replay_comment.parent = parent_obj new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.name = request.user new_comment.save() return redirect(post.get_absolute_url()) else: comment_form = CommentForm() return render(request, 'blog/blogpost_detail.html', {'blogpost': post, 'comments': comments, 'is_liked':is_liked, 'comment_form': comment_form})
def create_comment(request, id): # getting the post object from the URL param 'id'. # You can only create a comment for a post right? post = get_object_or_404(BlogPost, id=id) # this is the form to submit a comment form = CommentForm(request.POST or None) if request.method == 'POST': # checking if form is vaild on submission if form.is_valid(): comment = form.save(commit=False) # setting the proper post variable comment.post = post comment.save() print('form valid') # all was saved so we redirect return redirect(reverse('post_view')) # render the template return render(request, 'create_comment.html', {'form': form})