def p_read(request, post_id): post = get_object_or_404(Post, pk=post_id) session = request.session['user'] if request.method == 'POST': # POST 방식으로 호출 될 때 user_id = request.session.get('user') user = User.objects.get(username=user_id) comment = Comment(post=post, author=user_id) comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() return redirect(f'/posts/{post_id}/read') else: # GET 방식으로 호출 될 때 comment_form = CommentForm() context = { 'post': post, 'comment_form': comment_form, 'session': session } return render(request, 'read.html', context)
def view_post(request, slug=None): try: post = Post.objects.get(slug=slug) except ObjectDoesNotExist: raise Http404 if request.method == 'POST': comment = Comment(post=post) comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() comment_form1 = CommentForm() comments = post.comment_set.filter(status__exact='A') context = RequestContext(request, { 'post' : post, 'comment_form' : comment_form1, 'comments' : comments, 'len_comments' : len(comments)}) return render_to_response('post.html', context) else: return HttpResponseRedirect(post.get_absolute_url()) else: comments = post.comment_set.filter(status__exact='A') comment = CommentForm() context = RequestContext(request, { 'post' : post, 'comment_form': comment, 'comments':comments, 'len_comments' : len(comments)}) return render_to_response('post.html', context) return HttpResponse('/')
def add_comment(request, username, post_id): post = get_object_or_404(Post, id=post_id, author__username=username) form = CommentForm(request.POST or None) if form.is_valid(): form.instance.author = request.user form.instance.post = post form.save() return redirect('post', username=username, post_id=post_id)
def post(self, request, slug): post = self.get_object() if self.request.method == 'POST': form = CommentForm(self.request.POST) if form.is_valid(): form.instance.post_id = post.id form.save() else: form = CommentForm() return HttpResponseRedirect(reverse('posts:post-detail', kwargs={'slug': post.slug}))
def post(self, request, titleslug, comment_id): # This method posts the subcomment post = Post.objects.get(titleslug=titleslug) comment = Comment.objects.get(id=comment_id) form = CommentForm(request.POST) if form.is_valid(): form.save() return redirect("/posts/{}/{}".format(titleslug, comment_id)) else: return render(request, "posts/commentpage.html", {"form": form})
def post(self, request, titleslug): # This method posts the entered comment if valid and returns the user to # the post page post = Post.objects.get(titleslug=titleslug) user = User.objects.get(username=request.user) form = CommentForm(request.POST) if form.is_valid(): form.save() return redirect("/posts/{}".format(titleslug)) else: return render(request, "posts/postpage.html", {"form": form})
def add_reply_on_comment(request, username, post_id, comment_id): post = get_object_or_404(Post, id=post_id, author__username=username) comment = get_object_or_404(Comment, id=comment_id) form = CommentForm(request.POST or None) if form.is_valid(): form.instance.post = post form.instance.author = request.user form.instance.reply = comment form.save() return render(request, 'posts/comments.html', { 'post': post, 'form': CommentForm })
def newcomment(request,pk): if request.method=='POST': form=CommentForm(request.POST) if form.is_valid: c=form.save(commit=False) c.c_author=request.user c.post=Post.objects.get(pk=pk) form.save() return redirect('postdetail',pk=pk) else: form=CommentForm() return render(request,'createcomment.html',{'form':form})
def comment_create(request, id): post = get_object_or_404(Post, pk=id) form = CommentForm(request.POST) form.instance.post = post form.instance.content = request.POST.get('content') if form.is_valid(): form.save() messages.success(request, 'Thanks for your comment!!.') return redirect(reverse('post_details', kwargs={'id': post.id})) else: messages.warning(request, 'Sorry, something went wrong!!.') return redirect(post_list)
def add_comment_to_post(request, post_id): post = get_object_or_404(Post, pk=post_id) if request.method == "POST": comment = Comment(post=post) comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() path = '/posts/{}/'.format(post_id) return redirect(path) else: comment_form = CommentForm() return render(request, 'add_comment_to_post.html', {'comment_form':comment_form})
def post_detail_view(request, slug): post = Post.objects.get(slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.instance.post = post form.instance.author = request.user.userprofile form.save(commit=True) reverse_lazy('posts:detail', kwargs={'slug': slug}) form = CommentForm() return render(request, 'posts/detail.html', {'post': post, 'form': form})
def add_comment(request, post_id): """ Add comment to post with specified post_id and redirect to post page with post_id """ post = get_object_or_404(Post, pk=post_id) form = CommentForm(request.POST or None) if request.method == 'POST': if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post form.save() return redirect('post', post_id=post_id)
def post_view(request, username, post_id): post = get_object_or_404(Post, id=post_id, author__username=username) all_comments = post.comments.all() comment_form = CommentForm(request.POST or None) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.author = request.user new_comment.post = post new_comment.save() comment_form = CommentForm() # проверим, подписан ли пользователь на автора поста if request.user.is_authenticated: count = Follow.objects.filter(user=request.user, author__username=username).count() follows = True if count > 0 else False else: follows = False return render( request, 'post.html', { 'post': post, 'author': post.author, 'form': comment_form, 'following': follows, 'comments': all_comments })
def add_comment(request, username, post_id): user_req = get_object_or_404(User, username=username) post = get_object_or_404(Post, pk=post_id) comments = post.comments.all() if request.method == "POST": form = CommentForm(data=request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post comment.save() return redirect("post", username, post_id) return render(request, "post.html", { "user_req": user_req, "post": post, "form": form, "comments": comments }) form = CommentForm() return render(request, "post.html", { "user_req": user_req, "post": post, "form": form, "comments": comments })
def comment_create(request, pk): if request.method == 'POST': post = get_object_or_404(Post, pk=pk) # content = request.get('content') # # if not content: # return HttpResponse('댓글내용을 입력하세요.', status=400) # # Comment.objects.create( # author=request.user, # post=post, # content=content, # ) # return redirect() comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.author = request.user comment.save() messages.success(request, '댓글이 등록되었습니다') else: error_msg = '댓글 등록에 실패했습니다\n{}'.format( '\n'.join( [f'- {error}' for key, value in comment_form.errors.items() for error in value])) messages.error(request, error_msg) return redirect('post:post-detail', pk=pk)
def create_comments(request, **kwargs): # ipdb.set_trace() post = get_object_or_404(Post, slug=kwargs["slug"]) node_set = post.comment_set.all() if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): user_obj = request.user form.instance.fk_user = user_obj try: post_obj = Post.objects.get(slug=kwargs["slug"]) form.instance.fk_post = post_obj # if reply if kwargs.has_key("p_id"): form.instance.parent_cmt = Comment.objects.get(id=kwargs["p_id"]) form.save() form.save() node_set = post_obj.comment_set.all() except ObjectDoesNotExist: raise Http404 return render(request, "posts/post_detail.html", {"post": post_obj, "node_set": node_set}) else: form = CommentForm() if kwargs.has_key("p_id"): form.instance.parent_cmt = Comment.objects.get(id=kwargs["p_id"]) form_type = "reply_" else: form_type = "comment_" else: form = CommentForm() if kwargs.has_key("p_id"): form.instance.parent_cmt = Comment.objects.get(id=kwargs["p_id"]) form_type = "reply_" else: form_type = "comment_" return render( request, "posts/comment_form.html", {"form": form, "post": post, "node_set": node_set, "form_type": form_type} )
def post(self, request, pk, *args, **kwargs): user = request.user profile = Profile.objects.get(user=user) group = Group.objects.get(pk=pk) # Custom profanity words custom_badwords = CustomProfanity.objects.values_list('bad_word', flat=True) profanity.load_censor_words(custom_badwords) if 'submit_post_form' in request.POST: post_form = PostForm(request.POST, request.FILES) comment_form = None valid = post_form.is_valid() if profanity.contains_profanity( post_form.cleaned_data.get('content')): custom_profanity_error = 'Please remove any profanity/swear words. (Added by an admin. Contact an admin if you believe this is wrong.)' valid = False post_form.errors['content'] = custom_profanity_error if valid: post_instance = post_form.save(commit=False) post_instance.author = profile post_instance.group = group post_instance.save() return redirect('groups:view-group', pk=pk) elif 'submit_comment_form' in request.POST: comment_form = CommentForm(request.POST) post_form = None valid = comment_form.is_valid() if profanity.contains_profanity( comment_form.cleaned_data.get('body')): custom_profanity_error = 'Please remove any profanity/swear words. (Added by an admin. Contact an admin if you believe this is wrong.)' valid = False comment_form.errors['body'] = custom_profanity_error if valid: post_id = request.POST.get("post_id") comment_instance = comment_form.save(commit=False) comment_instance.user = profile comment_instance.post = Post.objects.get(id=post_id) comment_instance.save() return redirect(request.headers.get('Referer')) group_posts = Post.objects.filter(group=group) context = { 'profile': profile, 'group': group, 'posts': group_posts, 'post_form': post_form, 'comment_form': comment_form } return render(request, 'groups/view.html', context)
def create_comments(request, **kwargs): # ipdb.set_trace() post = get_object_or_404(Post, slug=kwargs['slug']) node_set = post.comment_set.all() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): user_obj = request.user form.instance.fk_user = user_obj try: post_obj = Post.objects.get(slug=kwargs['slug']) form.instance.fk_post = post_obj # if reply if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form.save() form.save() node_set = post_obj.comment_set.all() except ObjectDoesNotExist: raise Http404 return render(request, 'posts/post_detail.html', {'post': post_obj, 'node_set': node_set,}) else: form = CommentForm() if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form_type = 'reply_' else: form_type = 'comment_' else: form = CommentForm() if kwargs.has_key('p_id'): form.instance.parent_cmt = Comment.objects.get(id=kwargs['p_id']) form_type = 'reply_' else: form_type = 'comment_' return render(request, 'posts/comment_form.html', {'form': form, 'post': post, 'node_set': node_set, 'form_type': form_type})
def add_comment(request, username, post_id): post = get_object_or_404(Post, author__username=username, id=post_id) comment_form = CommentForm(request.POST or None) if comment_form.is_valid: instance_comment = comment_form.save(commit=False) instance_comment.post = post instance_comment.author = request.user instance_comment.save() return redirect("post", username=username, post_id=post_id)
def post(request, id): post = get_object_or_404(Post, id=id) if request.user.is_authenticated: PostView.objects.get_or_create(user=request.user, post=post) form = CommentForm(request.POST or None) if request.method == "POST": if form.is_valid(): form.instance.user = request.user form.instance.post = post form.save() return redirect(reverse("post_detail", kwargs={'id': post.pk})) context = { 'form': form, 'post': post, } return render(request, 'post.html', context)
def c_update(request, comment_id): comment = get_object_or_404(Comment, pk=comment_id) post = Post.objects.get(id=comment.post_id) # POST 방식으로 호출될 떄 if request.method == 'POST': comment_form = CommentForm(request.POST, instance=comment) if comment_form.is_valid(): comment_form.save() return render(request, 'p_detail.html', {'post':post}) # GET 방식으로 호출될 때 else: comment_form = CommentForm(instance=comment) return render(request, 'c_update.html', {'comment_form': comment_form})
def add_comment(request, username, post_id): post = get_object_or_404(Post, author__username=username, id=post_id) form = CommentForm(request.POST or None) if request.method != "POST" or not form.is_valid(): return redirect('posts:post', post.author, post.id) comment = form.save(commit=False) comment.post = post comment.author = request.user comment.save() return redirect(reverse("posts:post", args=[post.author, post.id]))
def comment(self, request, pk): post = get_object_or_404(Post, pk=pk) form = CommentForm(request.POST) if request.method == "POST": if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return HttpResponseRedirect( reverse('posts-details-page', args=[pk])) return render(request, 'post.html', {'form': form})
def add_comment_to_post(request, title_slug): post = get_object_or_404(Post, slug=title_slug) 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', title_slug=post.slug) else: form = CommentForm() return render(request, 'posts/comment_form.html', {'form': form})
def comment(request, slug): post = get_object_or_404(Post, slug=slug) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('posts:detail', slug=post.slug) else: form = CommentForm() return render(request, 'posts/comment.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() else: form = CommentForm() return render(request, 'posts/comment_form.html', {'form': form})
def add_comment(request, username, post_id): """Добавление комментария.""" author = get_object_or_404(User, username=username) post = get_object_or_404(author.author_posts, id=post_id) form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post_id = post_id comment.save() return redirect('post_view', username=username, post_id=post_id) return render(request, 'comments.html', {'form': form, 'post': post})
def add_comment(request, username, post_id): form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.post = get_object_or_404(Post, author__username=username, id=post_id) comment.author = request.user comment.save() return redirect('post_view', username, post_id) return render(request, 'new_comment.html', { 'form': form, })
def add_comment(request, username, post_id): author = get_object_or_404(User, username=username) post = get_object_or_404(author.posts, pk=post_id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post comment.save() return redirect('post_view', username, post.id)
def add_comment_to_post(request, id): post = get_object_or_404(Post, pk=id) 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('posts:detail', id=post.id) else: form = CommentForm() return render(request, 'my_blog/add_comment_to_post.html', {'form': form})
def detail(request, pk): post = Posts.objects.get(id=pk) comments = post.comments.all() form = CommentForm(request.POST or None) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.post = post instance.save() return HttpResponseRedirect(post.get_absolute_url()) context = {"post": post, "comments": comments, "form": form} return render(request, "detail.html", context)
def add_comment_to_post(request,pk): if request.method =="POST": post=get_object_or_404(models.Post,pk=pk) form=CommentForm(request.POST) if form.is_valid(): post_comment=form.save(commit=False) post_comment.author = request.user post_comment.text = request.POST["comment"] post_comment.post = post post_comment.save() return redirect("posts:post_detail",pk=post.pk) else: return redirect("posts:post_detail",pk=post.pk)
def post(request, id, *args, **kwargs): category_count = get_category_count() most_recent = Post.objects.order_by('-timestamp')[0:3] post = Post.objects.get(id=id) if request.user.is_authenticated: PostView.objects.get_or_create(user=request.user, post=post) form = CommentForm(request.POST or None) if request.method == "POST": if form.is_valid(): form.instance.user = request.user form.instance.post = post form.save() return redirect(reverse("post_detail", kwargs={'id': post.id})) return render(request, 'post.html', context={ 'form': form, 'post': post, 'most_recent': most_recent, 'category_count': category_count })
def detail_post(request, pk): """Detail Post.""" post = Post.objects.get(pk=pk) comment_form = CommentForm(request.POST or None) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() comment_form = CommentForm() return render(request, 'posts/detail_post.html', { 'post': post, 'form': comment_form })