Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何解决view blog.views.post#u细节问题';t返回HttpResponse对象。它没有返回任何结果_Python_Django - Fatal编程技术网

Python 我如何解决view blog.views.post#u细节问题';t返回HttpResponse对象。它没有返回任何结果

Python 我如何解决view blog.views.post#u细节问题';t返回HttpResponse对象。它没有返回任何结果,python,django,Python,Django,请任何人帮助我解决此错误:查看blog.views.post\u detail没有返回HttpResponse对象。它返回了None相反,我已经尝试过检查,但仍然无法解决它问题在于您对返回的缩进。如果,则应与第一个一致: def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post,status='published', publish__year = year, p

请任何人帮助我解决此错误:查看
blog.views.post\u detail
没有返回
HttpResponse
对象。它返回了
None
相反,我已经尝试过检查,但仍然无法解决它

问题在于您对
返回的缩进。如果
,则应与第一个
一致:

 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 ,})
当然,除非这个问题只是你的问题,而不是你的实际代码。但这将是检查/纠正的第一件事

 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 ,})
这是正确的代码。我已经修复了这个错误。
问题是我没有在if块下渲染。

我用代码修复了它。else块假定位于第一个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()
            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 ,})