视图comments.views.comment_create_view没有返回HttpResponse对象。它返回None而不是

时间:2016-02-24 01:52:58

标签: python django

我正在返回httpresponse对象,为什么我仍然会收到此错误?有人在我的代码中发现错误吗? 我只是不明白这一点,我已经设置了我的网址,错误就是从这个观点来说。我做错了什么?

def comment_create_view(request):
    if request.method == "POST" and request.user.is_authenticated():
        parent_id = request.POST.get('parent_id')
        post_id = request.POST.get("post_id")
        origin_path = request.POST.get("origin_path")
        try:
            post = Post.objects.get(id=post_id)
        except:
            post = None

        parent_comment = None
        if parent_id is not None:
            try:
                parent_comment = Comment.objects.get(id=parent_id)
            except:
                parent_comment = None

            if parent_comment is not None and parent_comment.post is not None:
                post = parent_comment.post

        form = CommentForm(request.POST)
        if form.is_valid():
            comment_text = form.cleaned_data['comment']
            if parent_comment is not None:
                # parent comments exists
                new_comment = Comment.objects.create_comment(
                    user=MyProfile.objects.get(user=request.user), 
                    path=parent_comment.get_origin, 
                    text=comment_text,
                    post = post,
                    parent=parent_comment
                    )
                return HttpResponseRedirect(parent_comment.get_absolute_url())
            else:
                new_comment = Comment.objects.create_comment(
                    user=MyProfile.objects.get(user=request.user), 
                    path=origin_path, 
                    text=comment_text,
                    post = post
                    )
                return HttpResponseRedirect(new_comment.get_absolute_url())
        else:
            return HttpResponseRedirect(origin_path)

1 个答案:

答案 0 :(得分:1)

并非所有代码路径都会导致返回HttpResponse。例如,如果方法不是POST或用户未经过身份验证,则视图不会返回任何内容(None)。