Django:登录只需1个参数(给定2个)

Kon*_*ler 0 python django

我最近试图回到Django,但是在尝试为练习创建登录页面时遇到了一些问题.我收到错误"登录需要1个参数(2个给定)",我不知道为什么.这是我的views.py:

from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext

def login(request):
    state = "Please log in below..."
    username = password = ''
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response('login.html',{'state':state, 'username': username},context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

这是我的login.html模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Log in</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
body{
    font-family:Arial,Helvetica,sans-serif;
    font-size: 12px;
}
</style>
</head>
<body>
    {{ state }}
    <form action="/login/" method="post">
        {% csrf_token %}
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        Username:
        <input type="text" name="username" value="{{ username }}" /><br />
        Password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In" />
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

rob*_*rob 14

您应该将视图的名称从login其他名称更改为其他名称,例如login_view,因为您当前尝试以递归方式调用它,而不是调用django.contrib.auth.login