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

Posted by Oleksandr Bolotov on Stack Overflow See other posts from Stack Overflow or by Oleksandr Bolotov
Published on 2009-07-15T22:28:34Z Indexed on 2010/05/01 12:57 UTC
Read the original article Hit count: 405

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about django

Related posts about mistakes