1

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?

1
  • 1
    You didn't actually return anything in the if clause. Commented Nov 20, 2015 at 10:48

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I am not authenticated. My intention with this view is to redirect logged-in users to /dashboard, and display a template for those who are not. Why would I need to create HttpResponseRedirect always?
@Milos: I'm only pointing out that there are cases in which your function doesn't return something and that's why you're getting that error. If the code doesn't do what you want then you'll need to change it.
Yeah, I realized that! :)
0

Return was missing from if block.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.