When the form is invalid, you need to render the view again, when you pass the same form object, it will display errors automatically, so no redirection in this case, this is how you make your view more clean:
def detail(request, blog_id):
b = get_object_or_404(Blog, pk=blog_id)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
poster = form.cleaned_data['poster']
comment = form.cleaned_data['comment']
comment_date = timezone.now()
b.comment_set.create(poster=poster, comment_date=comment_date, comment=comment)
return HttpResponseRedirect(reverse('blog:detail', args=(b.id,)))
else:
form = CommentForm()
return render(request, 'blog/detail.html', {
'blog': b,
'form': form,
})
Regarding tests, to go through the redirect, you need to add follow
argument in the post method (it's set to False by default):
c.post('/login/', {'name': 'fred', 'passwd': 'secret'}, follow=True)