2013-01-01 88 views
0

我最近試圖回到Django,但我有一些問題試圖做一個登錄頁面的做法。我收到錯誤「登錄只需要1個參數(給出2)」,我不知道爲什麼。這裏是我的views.py:Django:登錄只需要1個參數(2給出)

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> 

感謝您的幫助。

+0

在這裏看到:http://stackoverflow.com/questions/4547639/django-csrf-verification-failed – favoretti

+0

基本上你需要在模板中的'csrf_token'和'RequestContext' in'render_to_response' – favoretti

+0

@favoretti好吧,我只是將RequestContext移到了render_to_response函數中,現在我在嘗試登錄時遇到錯誤,說「login()只需要1個參數(給出2)」 –

回答

14

您應該將視圖的名稱從login更改爲其他內容,例如, login_view,因爲你目前正試圖遞歸調用它,而不是調用django.contrib.auth.login

+0

啊,這是有道理的。謝謝,這固定它:) –

相關問題