我試圖使用django.contrib.auth.login將用戶的ID存儲在會話中。但它沒有按預期工作。Django「login()只需要1個參數(給出2)」error
我發現了錯誤登錄()恰恰1個參數(2給出)
隨着登錄信息(用戶)我得到AttributeError的在/登錄/用戶」對象沒有屬性‘方法’
我用稍微modifyed例如形式http://docs.djangoproject.com/en/dev/topics/auth/:
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
def login(request):
msg = []
if request.method == 'POST':
username = request.POST['u']
password = request.POST['p']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
msg.append("login successful")
else:
msg.append("disabled account")
else:
msg.append("invalid login")
return render_to_response('login.html', {'errors': msg})
沒有什麼特別之處的login.html:
<html>
<head>
<title></title>
</head>
<body>
<form action="/login/" method="post">
Login: <input type="text" name="u">
<br/>
Password: <input type="password" name="p">
<input type="submit" value="Login">
</form>
{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
有沒有人有想法如何使登錄()的工作。
將工作,如果你重新命名你的觀點 – Evgeny 2009-07-15 22:36:14
只是我正在尋找的問題和答案。嘗試在django中註冊並運行時發生的幾個錯誤之一,以及使用Contexts vs RequestContexts和忽略csrf_tokens。 – chucksmash 2012-09-03 15:32:12