0

最近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))

これが私の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>

助けてくれてありがとう。

4

1 に答える 1

14

ビューの名前を別の名前に変更する必要がありloginます。たとえばlogin_view、現在、ビューを呼び出すのではなく、再帰的に呼び出そうとしているためです。django.contrib.auth.login

于 2013-01-01T16:27:17.467 に答える