Python 如果使用render\u to\u响应,是否需要一个HttpRequest对象?

Python 如果使用render\u to\u响应,是否需要一个HttpRequest对象?,python,django,http,authentication,Python,Django,Http,Authentication,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)

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})
我相信代码可以更有效地减少渲染,但这应该可以工作,对吗?

您缺少返回


您需要返回render\u to\u响应的响应。我还建议您对代码进行一些改进:

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})
唉,我需要多睡一会儿。非常感谢你,我为浪费你的时间而道歉。
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})