Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

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:&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 anybody have idea how to make login() work.

like image 281
Alex Bolotov Avatar asked Jul 15 '09 22:07

Alex Bolotov


People also ask

What does login function do in Django?

It provides a way to assign permissions to specific users and groups of users. It's used by the Django admin site, but you're welcome to use it in your own code.

What is Auth login in Django?

Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project's needs.

How does Django authentication work?

Django's built-in authentication system includes permissions, users, and groups. Django automatically creates four default permissions when you create a model—add, delete, change, and view. These permissions allow users to add, delete, modify, or view instances of the model respectively.


1 Answers

Your view function is also called login, and the call to login(request, user) ends up being interpreted as a attempt to call this function recursively:

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

To avoid it rename your view function or refer to the login from django.contrib.auth in some different way. You could for example change the import to rename the login function:

from django.contrib.auth import login as auth_login  ... auth_login(request, user) 
like image 191
sth Avatar answered Oct 13 '22 05:10

sth