0

Django 告诉我我的登录视图没有返回 HttpResponse 对象:

The view accounts.views.login didn't return an HttpResponse object.

但是,我在render_to_response()任何地方都在使用,并且视图无法在没有得到响应的情况下完成解析。这是代码:

def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    auth_login(request, user)
                    render_to_response('list.html')
                else:
                    error = "It seems your account has been disabled."
                    render_to_response('list.html', {'error': error})
            else:
                error = "Bad login information. Give it another go."
                render_to_response('list.html', {'error': error})
        else:
            error = "Bad login information. Give it another go."
            render_to_response('list.html', {'error': error})
    else:
        error = "Whoa, something weird happened. You sure you're using the form on our site?"
        render_to_response('list.html', {'error': error})

我确信代码可以更高效(更少的渲染),但这应该有效,对吗?

4

2 回答 2

5

你错过了回报

return render_to_response('list.html', {'error': error})
于 2012-04-26T22:07:24.713 回答
2

您需要返回 render_to_response 的响应。我还建议您进行一些代码改进:

def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    auth_login(request, user)
                    return render_to_response('list.html')
                else:
                    error = "It seems your account has been disabled."
            else:
                error = "Bad login information. Give it another go."
        else:
            error = "Bad login information. Give it another go."
    else:
        error = "Whoa, something weird happened. You sure you're using the form on our site?"
    return render_to_response('list.html', {'error': error})
于 2012-04-26T23:43:38.490 回答