def post(self, request, pk): # return HttpResponse(request.POST['entry_id']) form = CommentForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponse("Your comment submitted successfully") else: self.kwargs["forms"] = form return self.render_to_response(self.get_context_data(forms=form))
def forms(request, article_id): if request.POST: new_form = CommentForm(request.POST) if new_form.is_valid(): print(request.POST, new_form.errors, new_form.cleaned_data) comment = new_form.save(commit=False) comment.comment_article = Article.objects.get(id=article_id) new_form.save() return redirect('/article/get/%s/' % article_id)
def edit_comment(request, comid): context=RequestContext(request) com=Comment.objects.get(pk=comid) form= CommentForm(request.POST or None, instance=com) if form.is_valid(): form.save() return added(request) return render_to_response('blog/edit_comment.html', {'form':form, 'comid':comid}, context)
def post(self, request, *args, **kwargs): self.object = self.get_object() form = CommentForm(object=self.object, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(self.object.get_absolute_url()) context = self.get_context_data(object=self.object, form=form) return self.render_to_response(context)
def addcomment(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.comment_article = Articles.objects.get(id=article_id) form.save() request.session.set_expiry(60) request.session['pause'] = True return redirect('/articles/get/%s/' % article_id) # редирект на ту же статью на которой оставлен коммент
def comment_edit(request, entry_pk, pk): comment = get_object_or_404(Comment, pk=pk) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() return redirect("blog.views.detail", entry_pk) else: form = CommentForm(instance=comment) return render(request, "form.html", {"form": form})
def addcomment(request, article_id): return_path = request.META.get('HTTP_REFERER', '/') if request.POST and ("pause" not in request.session): form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comment_article = Article.objects.get(id=article_id) form.save() request.session.set_expiry(60) #ограничение ввода комментариев request.session['pause'] = True return redirect(return_path)
def addcomment(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(60) request.session["pause"] = True return redirect("/articles/get/%s/" % article_id)
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/")
def comment_new(request, post_pk): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.save() messages.debug(request, '새로운 댓글을 등록했습니다.') return redirect('post_detail', post_pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form':form, })
def comment_edit(request, post_pk, pk): # comment = Comment.objects.get(pk=pk) comment = Comment.get_object_or_404(Comment, pk=pk) if request.method == 'POST': form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() return redirect('blog,.post_detail', post_pk) else: form = CommentForm(instance=comment) return render(request, 'blog/comment_form.html', {'form': form, })
def comment_edit(request, id, comment_id): comment = get_object_or_404(Comment, id=comment_id) if request.method == 'POST': form = CommentForm(request.POST, request.FILES, instance=comment) if form.is_valid(): form.save() return redirect('blog:post_detail', id) else: form = CommentForm(instance=comment) return render(request, 'blog/post_form.html', { 'form': form, })
def comment_edit(request, post_pk, pk): # comment = Comment.objects.get(pk=pk) # comment = get_object_or_404(Comment, pk=pk) comment = request.instance if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() return redirect(comment.post) else: form = CommentForm(instance=comment) return render(request, "blog/comment_form.html", {"form": form})
def comment_form(request): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.save(commit=True) return index (request) else: print (form.errors) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form':form})
def comment(request, post_id): p = get_object_or_404(Post, pk=post_id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('post:detail', args=(p.id,))) else: form = CommentForm(request.POST) c = {'form':form} return HttpResponseRedirect(reverse('post:detail', args=(p.id,))) #return HttpResponse("You're looking at the comments of post %s." % c) return HttpResponseRedirect('/post/' % post_id)
def comment_edit(request, post_pk, pk): comment = get_object_or_404(Comment, pk=pk) if request.method == 'POST': form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() messages.success(request, 'updated comment') return redirect('blog.views.detail', post_pk) else: form = CommentForm(instance=comment) return render(request, 'form.html', { 'form': form, })
def comment_edit(request, post_pk, pk): comment = get_object_or_404(Comment, pk=pk) if request.method == 'POST': form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() messages.success(request, '댓글이 수정되었습니다.') return redirect(comment.post) else: form = CommentForm(instance=comment) return render(request, 'form.html', { 'form': form, })
def comment_edit(request, post_pk, pk): comment = get_object_or_404(Comment, pk=pk) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() messages.success(request, "댓글이 수정되었습니다.") # return redirect('blog:detail', post_pk) # models.py에서 get_absolute_url 함수가 정의된 경우 post만 넘겨도 가능하다. return redirect(comment.post) else: form = CommentForm(instance=comment) return render(request, "form.html", {"form": form})
def comment(req): if req.method == 'POST': form = CommentForm(req.POST) # 换成表单 if form.is_valid(): comment = form.save() return HttpResponseRedirect('/comment/') comment_list = Comment.objects.all() paginator = Paginator(comment_list, 10) num_pages = paginator.num_pages pages = range(1, num_pages + 1) width = num_pages * 34 # Make sure page request is an int. If not, deliver first page. try: page = int(req.GET.get('page', '1')) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: comments = paginator.page(page) except (EmptyPage, InvalidPage): comments = paginator.page(paginator.num_pages) return render(req, 'comment.html', { 'comments': comments, 'tests': comment_list, 'pages': pages, 'current_page': int(page), 'width': width, })
def comment(request, post_id): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): if not request.user.is_authenticated(): r = captcha.submit(request.POST['recaptcha_challenge_field'], request.POST['recaptcha_response_field'], settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR']) if not r.is_valid: return redirect('/blog/'+post_id+'/') # Invalid form entry f = form.save(commit=False); f.post_id = post_id if request.user.is_authenticated(): f.author_id = request.user.id f.author_name = '' f.author_email = '' f.save() email_msg = 'You have a new comment at http://www.jessicasteiber.com/blog/'+post_id+'/#comments' email_from = '*****@*****.**' email_to = ['*****@*****.**', '*****@*****.**'] # Send email notification send_mail('New Blog Comment - JessicaSteiber.com', email_msg, email_from, email_to) return redirect('/blog/'+post_id+'/#comments') # Go back to blog with new comment else: return redirect('/blog/'+post_id+'/') # Invalid form entry else: return redirect('/blog/'+post_id+'/') # Go back to blog
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) comments = post.comments.filter(active=True) if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() # XXX: fix this, can't find post after submitting return HttpResponseRedirect(reverse('blog:post_detail', kwargs={'year': year, 'month': month, 'day': day, 'post': post})) else: comment_form = CommentForm() post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id) similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags', '-publish')[:4] return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'comment_form': comment_form, 'similar_posts': similar_posts})
def add_comment(request, slug): """Add a new comment.""" p = request.POST post = Post.objects.get(slug=slug) author_ip = get_ip(request) if p.has_key("body") and p["body"]: author = "Anonymous" if p["author"]: author = p["author"] comment = Comment(post=post) cf = CommentForm(p, instance=comment) cf.fields["author"].required = False comment = cf.save(commit=False) comment.author = author comment.author_ip = author_ip comment.save() messages.success( request, "Thank you for submitting a comment. It will appear once reviewed by an administrator." ) else: messages.error(request, "Something went wrong. Please try again later.") return HttpResponseRedirect(post.get_absolute_url())
def article(request, article_pk, slug=None): article = get_object_or_404(Article, pk=article_pk, is_draft=False) # If slug is incorrect, "redirect friendly" to the correct link. if not slug or slug != article.slug: return http.HttpResponsePermanentRedirect(article.get_absolute_url()) if request.method == "POST": user = request.user if user.is_anonymous(): return http.HttpResponseForbidden() comment_form = CommentForm(request.POST, user=user) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.author = user if user.is_staff and user.is_superuser: comment.is_moderated = True comment.save() return redirect(comment.get_absolute_url()) else: comment_form = CommentForm() return render_to_response( "article.html", { "page_title": article.title, "article": article, "comment_form": comment_form, "comment_pk": request.GET.get("comment_pk"), }, context_instance=RequestContext(request), )
def post_detail(request, slug): post = get_object_or_404(Post.pub, slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.ip_address = request.META.get("REMOTE_ADDR", None) comment = form.moderate(comment) comment.save() email_body = "%s posted a new comment on the post '%s'." mail_managers("New comment posted", email_body % (comment.user_name, post)) return redirect('blog.views.post_detail', slug=post.slug) else: form = CommentForm() return render_to_response('blog/post_detail.html', { 'post': post, 'form': form, })
def detail(request, p_id): context = RequestContext(request) tags = Tag.objects.all() context_dict = {'tags': tags} post = get_object_or_404(Post, id=p_id) context_dict['post'] = post comments = Comment.objects.filter(post=post) context_dict['comments'] = comments if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('detail', p_id=p_id) else: form = CommentForm() context_dict['comment_form'] = form return render_to_response('blog/detail.html', context_dict, context)
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) # List of active comments for this post comments = post.comments.filter(active=True) if request.method == 'POST': # A comment was posted comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object but don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() # List of similar poste post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id) similar_posts = similar_posts.annotate(same_tags =Count('tags')).order_by('-same_tags', '-publish')[:4] return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments , 'comment_form': comment_form, 'similar_posts': similar_posts})
def submit_comment(request, slug): """ Process the comment form """ post = get_object_or_404(Post.objects, slug=slug) form = CommentForm(request.POST, auto_id="%s", prefix="CommentForm") if form.is_valid() and post.allow_comments: comment = form.save(commit=False) comment.ip_address = request.META.get("REMOTE_ADDR", None) comment.post = post comment.save() if comment.published: request.notifications.create(_("Your comment has been posted!"), "success") else: request.notifications.create(_("Your comment was posted, but it won't be published until approved."), "warning") if request.is_ajax(): response = render_to_response("blog/comment_submit.json", { "errors": None, "comment": comment, "post": post, "form": form, }, context_instance=RequestContext(request), mimetype="application/json") else: response = HttpResponseRedirect(post.get_absolute_url()) if form.cleaned_data.get("remember", False): response.set_cookie("comment_author", value=comment.author) response.set_cookie("comment_email", value=comment.email) response.set_cookie("comment_url", value=comment.url) else: response.delete_cookie("comment_author") response.delete_cookie("comment_email") response.delete_cookie("comment_url") return response else: request.notifications.create(_("There were some errors with your comment submission."), "error") if request.is_ajax(): template = "blog/comment_submit.json" mimetype = "application/json" errors = simplejson.dumps(form.errors, cls=LazyEncoder, ensure_ascii=False) else: template = "blog/comment_submit.html" mimetype = "text/html; charset=utf-8" errors = form.errors return render_to_response(template, { "errors": errors, "comment": None, "post": post, "form": form, }, context_instance=RequestContext(request), mimetype=mimetype)
def comment_edit(request, freeboard_pk, pk): comment = get_object_or_404(Comment, pk=pk) if comment.author.user != request.user: messages.warning(request, "권한이 없습니다") return redirect("blog:freeboard_detail", freeboard_pk) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() messages.success(request, "수정완료!") return redirect("blog:freeboard_detail", freeboard_pk) else: form = CommentForm(instance=comment) return render(request, 'blog/comment_form.html', {'form': form})
def view_post(request, slug): post = get_object_or_404(Post, slug=slug) form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect(request.path) return render_to_response('blog_post.html',{'post': post,'form': form,},context_instance=RequestContext(request))
def article_detail(request, pk): article = get_object_or_404(Article, pk=pk) form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.article = article comment.save() return redirect(article) return render(request, 'blog/articles/article_detail.html', {'form': form, 'object': article})
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,'blog/comment_form.html',{'form':form})
def AddComment(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_view.html", {"form": form})
def add_comment_to_post(request,pk):#pk, link comment to the specific post,想成在specific pk post 物件, 按下 add comment post = get_object_or_404(Post, pk=pk) #grab the specific pk post or 404 can't find, if request.method == 'POST': form = CommentForm(request.POST)#request.POST: hit the post request if form.is_valid(): comment = form.save(commit=False) #via comment_form, model = comment, commit=False hasn’t yet been saved to the database comment.post = post #model, comment foreign key 綁定特定post, commit=False cause we want to do some custom processing comment.save()#save to database 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': # if the user has commented and clicked on post comment form = CommentForm(request.POST) if form.is_valid(): # if the entries in the comment form is valid comment = form.save(commit=False) # store it temporarily comment.post = post # associating the comment wala post to the actual post comment.save() #save it to the post 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 # the post on the lhs refers to the foreign key in the Comment model and the post on the rhs refers to the instance of that function 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, 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.author = request.user comment.post = post comment.save() return redirect('post_detail', slug=post.slug) else: form = CommentForm() return render(request, "blog/post_detail.html", {"form": form})
def newcomment(request, post_id): post = get_object_or_404(Post, pk=post_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('detail', post.pk) else: form = CommentForm() return render(request, 'blog/newcomment.html', {'form': form})
def post(request, id): category_count = get_category_count() 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.id})) context = { 'category_count': category_count, 'post': post, 'form': form, } return render(request, 'post.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 model there is a post which is a fk, #this statement is assigning this->post object to the table->post and hence filling all entities in Table comment.save() return redirect('blog:post_detail', pk=post.pk) else:#ie a get request form=CommentForm() return render(request,'blog/comment_form.html', {'form':form})
def DetailViewPost(request, pk): posts = get_list_or_404(Post, pid=pk) comments = Comment.objects.filter(post_id=pk).exclude( approved_comment__isnull=True).order_by('-created_date') if request.user.is_authenticated: if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): content = form.save(commit=False) content.post_id = pk content.author_id = request.user.id form.save() messages.success( request, f'Your comment has been posted but not been Approved') return redirect('blog:post_detail', pk=pk) else: form = CommentForm() else: form = CommentForm() context = {'posts': posts, 'comments': comments, 'form': form} return render(request, 'post_detail.html', context)
def post_comment(request, slug): post = get_object_or_404(Post, slug=slug) if request.method != 'POST': form = CommentForm() context = {'form': form, 'post': post} return render(request, 'blog/post_comment.html', context) else: form = CommentForm(data=request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('blog:post_detail', slug=post.slug)
def add_comment_to_post(request, pk): # Get the object Or 404 error in this case (Get The Post Object Or 404 Error) 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 #in models.py Comment has an attribute called post which is linked by Foreignkey to Post . this tells us to save comment.post by making it equal to 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"): userform = CommentForm(request.POST) if userform.is_valid(): comment = userform.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: userform = CommentForm return render(request, 'post_detail.html', {'userform': userform})
def post(request, pk): post = get_object_or_404(Post, pk=pk) most_recent = Post.objects.order_by('-timestamp')[0:3] category_count = get_category_count() form = CommentForm(request.POST or None) if request.user.is_authenticated: PostView.objects.get_or_create(user=request.user, post=post) if request.method == "POST": if form.is_valid(): form.instance.post = post form.instance.user = request.user form.save() return redirect(reverse('post', kwargs={'pk': post.pk})) return render( request, 'post.html', { 'post': post, 'most_recent': most_recent, 'category_count': category_count, 'form': form })
def add_comment_to_post(request, pk): # first import get_object_or_404 and redirect 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 # connect the comment to the post's pk 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.save() messages.success(request, "Thanks for your feedback. Your comment will appear once approved") 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): blog_post = get_object_or_404(BlogPost, pk=pk) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.parent_post = blog_post comment.save() return redirect("blog:blog_post_view", pk=blog_post.pk) else: form = CommentForm() return render(request, "blog/comment_form.html", context={"form": form})
def comment_edit(request, pk): comment = get_object_or_404(Comment, pk=pk) if request.method == "POST": form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) # comment.post = post comment.save() return redirect('post_detail', pk=comment.post.pk) else: form = CommentForm(instance=comment) return render(request, 'blog/comment_edit.html', {'form': form})
def comment_to_post(request,pk): post = get_object_or_404(Post,pk=pk) comments=post.comments.filter(active=True) 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() return redirect('post_detail',pk=post.pk) else: form = CommentForm() return render(request,'blog/comment_form.html',{'form':form,'post':post,'comments':comments})
def lire_article(request, slug): """ Affiche un article complet, sélectionné en fonction du slug fourni en paramètre """ article = get_object_or_404(Article, slug=slug) commentaires = article.commentaires.filter(is_visible=True) if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment_form.save() return redirect(lire_article, slug=slug) else: comment_form = CommentForm() return render( request, 'blog/lire_article.html', { 'article': article, "commentaires": commentaires, "comment_form": comment_form })
def view_comment(request, id_title): form = CommentForm(request.POST or None) bool_sent = False item = Item.objects.get(title=id_title) comments_list = item.comment_set.all() if form.is_valid(): comment = form.save(commit=False) comment.item = item comment.save() bool_sent = True return render(request, 'blog/comment.html', locals())
def post(self, request, id): article = Article.objects.get(id=id) author = request.user if request.user.is_authenticated and request.user.is_verify: if request.method == 'POST': comment = CommentForm(request.POST) if comment.is_valid(): newComment = comment.save(commit=False) newComment.post = article newComment.author = author newComment.body = comment.cleaned_data['body'] newComment.save() return redirect('detail_url', id)
def add_comment_to_post(request,pk): # a pk is send too as we want to add comment only to specific post post = get_object_or_404(Post,pk=pk) # get that POST obj or not found if request.method == 'POST': # someone actually filled int he form or hit enter 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 edit_comment(request, pk): comment = get_object_or_404(Comment, pk=pk) form = CommentForm(request.POST or None, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.save() return redirect('blog-detail', pk=comment.blog.pk) context = {"form": form} return render(request, 'blog/add_comment.html', context)
def post(self, request, *args, **kwargs): post = self.get_post(**kwargs) comments = post.comments.filter(active=True) comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() similar_posts = self.get_similar_post(post) return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'comment_form': comment_form, 'similar_posts': similar_posts})
def add_comment_to_post(request, pk): # if object no found, go 404 - but when objects found keep that in the background (we don't display it) post = get_object_or_404(Post, pk=pk) # here Post is our model if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post # to connect the comment to the post - make the comment.post (ForeignKey) equals to the current post object (1st var of the function) comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def comment_create_view(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('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'add_comment.html', {'form': form})
def add_comment_to_post(request, pk): # Get the object with the provided pk or return 404 (not found) 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 #the model has post attribute that needs to be filled explicitly, so we are not commiting t the first place comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})