Django "login() takes exactly 1 argument (2 given)" error
- by Oleksandr Bolotov
I'm trying to store the user's ID in the session using django.contrib.auth.login . But it is not working not as expected.
I'm getting error login() takes exactly 1 argument (2 given)
With login(user) I'm getting AttributeError at /login/ User' object has no attribute 'method'
I'm using slightly modifyed example form 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})
there's nothing special about 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>
Does anybody have idea how to make login() work.