how can i solve The view blog.views.post_detail didn't return an HttpResponse object. It returned None instead

 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)
    new_comment = None 
    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()
        else:
            comment_form = CommentForm()
        return render (request, 'blog/post/detail.html', {'post':post,'comments':comments,'new_comment':new_comment, 'comment_form': comment_form ,})

please can anyone help me to solve this error :The view blog.views.post_detail didn't return an HttpResponse object. It returned None instead, i have tried to check but i cant still solve it

Upvotes: 0

Views: 167

Answers (2)

MeL
MeL

Reputation: 1269

The problem is with your indentation for return. It should be in line with the first if:

 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)
    new_comment = None 
    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()
    else:
       comment_form = CommentForm()
    return render (request, 'blog/post/detail.html', {'post':post,'comments':comments,'new_comment':new_comment, 'comment_form': comment_form ,})

Unless of course this problem is only in your question and not your actual code. But that would be the first thing to check / correct.

Upvotes: 1

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)
    new_comment = None 
    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()
            return HttpResponseRedirect('/success/')
    else:
        comment_form = CommentForm()
    return render (request, 
                    'blog/post/detail.html', 
                    {'post':post,
                    'comments':comments,
                    'new_comment':new_comment, 
                    'comment_form': comment_form ,})

this is the correct code. i have fixed the bug. the problem was i did not render under the if block.

Upvotes: 0

Related Questions