Django "login () takes exactly 1 argument (2 given)" error

I am trying to save a user id in a session using django.contrib.auth.login. But it does not work as expected.

I get an error login () takes exactly 1 argument (2)

With login (user) I get an AttributeError object at / login / User 'does not have the attribute' method '

I use a slightly modified example of the 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 is nothing special about login.html:

 <html> <head> <title></title> </head> <body> <form action="/login/" method="post"> Login:&nbsp; <input type="text" name="u"> <br/> Password:&nbsp; <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 anyone have an idea how to make login () work.

+82
django
Jul 15 '09 at 22:28
source share
3 answers

Your view function is also called login , and the call to login(request, user) ends with an interpretation as an attempt to call this function recursively:

 def login(request): ... login(request, user) 

To avoid renaming your view function, or refer to login from django.contrib.auth several ways. You can, for example, change the import to rename the login function:

 from django.contrib.auth import login as auth_login ... auth_login(request, user) 
+225
Jul 15 '09 at 22:32
source share

One possible fix:

 from django.contrib import auth def login(request): # .... auth.login(request, user) # ... 

Now your view name does not overwrite the django view name.

+15
Jul 15 '09 at 22:41
source share

Another way:

 from django.contrib.auth import login as auth_login 

then call auth_login(request, user) instead of login(request, user) .

+9
Jul 01 2018-11-17T00:
source share



All Articles