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 ,})