Re: authenticate=None mistery: can't authenticate from the web but it works from the command line

23 views
Skip to first unread message

Joseph Mutumi

unread,
Aug 19, 2012, 5:51:51 PM8/19/12
to django...@googlegroups.com
Hello,

I believe its a simple typo!

authenticate(user=usuario, password=clave)

should be

authenticate(username=usuario, password=clave)

Regards

On Sun, Aug 19, 2012 at 3:35 PM, Jorge Garcia <jorgega...@gmail.com> wrote:
Hi there. Im doing my first login form for an existing Django application. The thing is that when I give the correct usr/pswd from the web,  django.contrib.auth.authenticate returns systematically None. However, when I try the same thing from the Django shell it works.  I'm working with a "john" user created from the Django admin application, using the "password chang" form. Here's the code, and the Django command line output. 

My application and me will be eternally thankful for your help.

++COMMAND LINE
python manage.py shell
>>> from django.contrib.auth import authenticate
>>> user = authenticate(username='john', password='johnpassword')
>>> print user
john

++VIEWS.PY
from django.contrib.auth.models import User

def index(request):
    return render_to_response('unoporuno/index.html', None, context_instance=RequestContext(request))

def login_cidesal(request):
    usuario = request.POST['usuario']
    clave = request.POST['clave']
    user = authenticate(user=usuario, password=clave)
    return HttpResponse("logging in user:" + usuario + " with password:" + clave + " and authenticate result=" + str(user))

++URLS.PY
urlpatterns = patterns('unoporuno.views', 
                   url(r'^$','index'),
                   url(r'login/', 'login_cidesal'),

++INDEX.HTML
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}style1.css"/>
</head>
<body> 
<form method="post" action="/unoporuno/login/">
{% csrf_token %}
<div align="center">
<img src="{{ STATIC_URL }}cidesal.jpg" height="140" width="140" border="0"/>
<h1>UnoporunO</h1>
<h4>Buscador de personas especializado en movilidad profesional</h4>
<br/>
Usuario: <input type="text" name="usuario" size="16" /><br/><br/>
Clave : <input type="password" name="clave" size="16" /><br/>
<p><input type="submit" value="Login" /></p>
</div>
</body>
</html>

++WEB RESULT TYPING usuario=john clave=johnpassword
logging in user:john with password:johnpassword and authenticate result=None

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/1UKsQlJ5OB8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Tom Evans

unread,
Aug 22, 2012, 6:37:42 AM8/22/12
to django...@googlegroups.com
In addition to the 'user'/'username' typo, this login view would not
log a user in. In order to log a user in, you must verify their
credentials by calling django.contrib.auth.authenticate(), which will
either return an authenticated user or None.

Once the credentials are verified, you must then call
django.contrib.auth.login() to persist login information in the users
session. Without calling login(), the user will only be authenticated
on the single request in which they authenticated.

More documentation on how to use authenticate and login is here:

https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.login

Cheers

Tom

Jorge Garcia

unread,
Aug 22, 2012, 8:49:40 AM8/22/12
to django...@googlegroups.com, teva...@googlemail.com
Indeed it was a typo! 
Now that I have a valid user instance, I will use django.contrib.auth.login() to have a session. 

Thank you both for your help ;)
Reply all
Reply to author
Forward
0 new messages