87

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.

3
  • 4
    will work if you rename your view
    – Evgeny
    Jul 15, 2009 at 22:36
  • Just the question and answer I was looking for. One of several mistakes I was making while trying to get logins up and running in django, along with using Contexts vs RequestContexts and leaving out csrf_tokens.
    – chucksmash
    Sep 3, 2012 at 15:32
  • here you can find a tutorial about Django users lowcoupling.com/post/71289666862/django-the-user-tutorial there is also a GitHub project you can clone and check Dec 28, 2013 at 17:15

3 Answers 3

239

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)
1
  • Wow, I was browsing through Django's source code, and trying every older version available, thinking I was going insane
    – Mojimi
    Jul 3, 2017 at 3:46
16

One possible fix:

from django.contrib import auth

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

Now your view name doesn't overwrite django's view name.

8

Another way:

from django.contrib.auth import login as auth_login

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.