errorous
errorous

Reputation: 1071

Django: value error. View didn't return HTTPResponseObject

This is the view in question:

def index(request):
    if request.user.is_authenticated():
        HttpResponseRedirect('/dashboard')
    else:
        return render(request, 'index.html')

And when I get to the index page, I get this:

ValueError at /

The view foobar.views.index didn't return an HttpResponse object. It returned None instead.

What could be the problem here?

Upvotes: 1

Views: 81

Answers (2)

Waqas Javed
Waqas Javed

Reputation: 773

Return was missing from if block.

def index(request):
if request.user.is_authenticated():
    return HttpResponseRedirect('/dashboard')
else:
    return render(request, 'index.html')

Upvotes: 0

Simeon Visser
Simeon Visser

Reputation: 122376

Are you authenticated when viewing that page? You'd need to make sure you also return the HttpResponseRedirect object in that case:

def index(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/dashboard')
    else:
        return render(request, 'index.html')

Otherwise you create a HttpResponseRedirect object but you don't return it so that means the code will continue and the function will return None (which is the default return value of all functions / methods in Python).

Upvotes: 1

Related Questions