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 に答える