def post_detail_view(request, slug): post = get_object_or_404(Post.objects.published(), slug=slug) post.increment_views_counter() is_liked = post.is_liked(request) if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment_form.save_with_post(post) return redirect(reverse('blog:post_detail', args=[post.slug])) else: comment_form = CommentForm() return render(request, 'blog/post_detail.html', { 'post': post, 'comment_form': comment_form, 'is_liked': is_liked })
def test_save_with_post(self): post = self.create_post() data = {'author': 'author', 'content': 'content'} form = CommentForm(data=data) comment = form.save_with_post(post) self.assertEqual(list(post.comments.all())[-1], comment)