def post_detail(request, post_id, message=""): post = get_object_or_404(Post, pk=post_id) other_posts = Post.objects.filter(category=post.category).filter( published=True).exclude(title=post.title) comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') if request.method == "POST": comment_form = comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() return HttpResponseRedirect( reverse( 'post-detail-message', args=[post.pk, 'Your comment has been posted'], ) + '#comments') else: comment_form = CreateCommentForm() context = { 'post': post, 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'other_posts': other_posts, 'comments': comments, 'comment_form': comment_form, 'message': message } return render(request, 'blog/post_detail.html', context)
def post(self, request, article_id): print request.POST new_comment = CreateCommentForm(request.POST) if new_comment.is_valid(): comment = new_comment.save(commit=False) comment.user = request.user comment.article = Article.objects.get(id=article_id) comment.save() return HttpResponseRedirect('/blog/view/'+article_id)
def post_detail(request, post_id): post = get_object_or_404(Post, pk=post_id) post_same_category = Post.objects.filter(published=True, category=post.category) \ .exclude(pk=post.id) comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment_form.save() else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'post_same_category': post_same_category, 'comments': comments, 'comment_form': comment_form, } return render(request, 'blog/post_detail.html', context)
def test_create_comment(self): ''' コメント作成のテスト ''' form = CreateCommentForm({ 'name': '名無し', 'text': 'コメントのテストです。' }, post=self.published_post) self.assertTrue(form.is_valid()) comment = form.save() self.assertEqual(comment.name, '名無し') self.assertEqual(comment.text, 'コメントのテストです。') self.assertEqual(comment.post, self.published_post)
def post_detail_view(request, *args, **kwargs): pk = int(kwargs.get('id')) if request.method == 'GET': form = CreateCommentForm() post = BlogPost.objects.get(id=pk) comments = post.comments.all() tags = post.tags.all() return render(request, 'post_detail.html', locals())
def add_comment_view(request, *args, **kwargs): post_id = int(kwargs.get('id')) post = BlogPost.objects.get(id=post_id) if request.method == 'POST': form = CreateCommentForm(request.POST) if form.is_valid(): form.cleaned_data['post'] = post form.save() else: form = CreateCommentForm() return redirect('blog:post-detail', id=post_id)
def post_detail(request, post_pk): if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): post = get_object_or_404(Post, pk=post_pk) author = comment_form.cleaned_data['author'] email = comment_form.cleaned_data['email'] text = comment_form.cleaned_data['text'] new_comment = Comment(post=post, author=author, email=email, text=text) new_comment.save() messages.info(request, 'Comment created.') return redirect(reverse('blog:post_detail', kwargs={'post_pk': post_pk})) else: comment_form = CreateCommentForm() return render(request, 'blog/post_detail.html', {'post': get_object_or_404(Post, pk=post_pk), 'comment_form': comment_form, 'comments': Comment.objects.filter(post_id=post_pk)})
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) posts_same_category = Post.objects.filter(published=True, category=post.category)\ .exclude(pk=post_id) comments = post.comments.all().order_by('created_at') if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() args = [post.pk, 'Your comment has been posted!'] return HttpResponseRedirect( reverse('post-detail-message', args=args) + '#comments') else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'posts_same_category': posts_same_category, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) related = model_helpers.get_related_posts(post) comments = model_helpers.get_comments(post) if request.method == 'POST': comment_form = CreateCommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() args = [post.pk, 'Your comment has been submitted.'] return HttpResponseRedirect(reverse('post-detail-message', args=args) + '#comments') else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'related': related, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def post_detail(request, post_id, message=''): post = get_object_or_404(Post, pk=post_id) # get_object_or_404 allows to get a post # variable that contents list of post in same category posts_same_category = Post.objects.filter(published=True, category=post.category) \ .exclude(pk=post_id) # short comment by status and order by created date comments = post.comments.exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') # treatment of form comment # tests if the method HTTP used is a post request so form was sent if request.method == 'POST': comment_form = CreateCommentForm(request.POST) # request.POST a parameter give to constructor that gather data content in form # checks form validity if comment_form.is_valid(): comment = comment_form.save(commit=False) # associate comment to a post by FK comment.post = post # save in BDD comment.save() args = [post.pk, 'Thanks, your comment has been posted'] # display message to user to confirm submit of form content # post-detail-message is url of redirection if a post wa submitted return HttpResponseRedirect( reverse('post-detail-message', args=args) + '#comments') # to place browser near the id that represents comment that posted else: comment_form = CreateCommentForm() context = { 'navigation_items': navigation.navigation_items(navigation.NAV_POSTS), 'post': post, 'posts_same_category': posts_same_category, 'comments': comments, 'comment_form': comment_form, 'message': message, } return render(request, 'blog/post_detail.html', context)
def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx['comment_form'] = CreateCommentForm() ctx['reply_form'] = CreateReplyForm() ctx['breadcrumb'] = [{'name': self.object.title, 'link': None}] return ctx
def recipe(request, slug, message=''): post = get_object_or_404(Post, slug=slug) preferred_posts = Post.objects.filter( published=True).order_by('-likes')[:5] posts_same_category = Post.objects.filter( category__in=post.category.all(), published=True).exclude(slug=slug) comments = post.comments.filter(parent__isnull=True).exclude( status=Comment.STATUS_HIDDEN).order_by('created_at') newsletter = '' if request.method == 'POST': newsletter_form = NewsletterForm(request.POST) if newsletter_form.is_valid(): newsletter_form.save() newsletter = 'Merci ! Votre inscription à la newsletter est bien prise en compte !' new_subscription = request.POST.get('email') send_mail( 'Nouvelle inscription à la newsletter sur le site dianecooking.com', f"L'adresse mail : {new_subscription} vient de s'inscrire à la newsletter.", new_subscription, ['*****@*****.**'], ) else: newsletter_form = NewsletterForm() if request.method == 'POST' and not newsletter_form.is_valid(): comment_form = CreateCommentForm(data=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: reply_comment = comment_form.save(commit=False) reply_comment.parent = parent_obj new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() message = 'Votre commentaire a bien été enregistré !' author_name = request.POST.get('author_name') text = request.POST.get('text') default_email = '*****@*****.**' send_mail( 'Nouveau commentaire depuis le site dianecooking.com', f"{author_name} a déposé le commentaire suivant: '{text}', sur la publication : '{post.title}'.", default_email, ['*****@*****.**'], ) comment_form = CreateCommentForm() else: comment_form = CreateCommentForm() context = { 'post': post, 'preferred_posts': preferred_posts, 'posts_same_category': posts_same_category, 'comments': comments, 'comment_form': comment_form, 'newsletter_form': newsletter_form, 'message': message, 'newsletter': newsletter, } return render(request, 'blog/recipe.html', context)