mike braa
mike braa

Reputation: 637

The view comments.views.comment_create_view didn't return an HttpResponse object. It returned None instead

I'm returning httpresponse object, why am I still getting this error?can someone spot false in my code? I just don't understand this, I have set my url right and the error is saying it's from this view. What am I doing wrong?

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)

Upvotes: 0

Views: 38

Answers (1)

alecxe
alecxe

Reputation: 474141

Not all of the code paths result into HttpResponse being returned. For example, if the method is not POST or the user is not authenticated, the view returns nothing (None).

Upvotes: 1

Related Questions