Reputation: 53
I'm really missing what is going on here. Django 1.3.
Symptom: the Authentication is simply busted. I've reduced the problem to this:
./manage.py shell
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>from django.contrib.auth.models import User
>>> users = User.objects.all()
>>> users
[<User: jal>]
>>> users[0].set_password('whatever');
>>> users[0].save()
>>> user = auth.authenticate(username="jal", password="whatever")
>>> print user
None
I'm at a loss on this.
Edit:
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username="jal")
>>> print user
jal
>>> user.check_password('whatever')
False
I'm using the django.contrib.auth.backends.ModelBackend .
Edit two:
>>> print user
jal
>>> print user.is_active
True
Edit three:
Alasdair is right - thank you.
>>> users = User.objects.all()
>>> user = users[0]
>>> user.set_password('whatever')
>>> user.save()
>>> u2 = auth.authenticate(username="jal", password="whatever")
>>> print u2
jal
This still leaves the mystery as to why it is broken for actually accessing the Django admin GUI, which is why I was headed down this road in the first place. It gives me "Please enter a correct username and password. Note that both fields are case-sensitive" whether or not the password is correct.
Upvotes: 4
Views: 6388
Reputation: 308769
You're having this problem because Django querysets are lazy. Each time you access users[0]
, Django returns a different object.
To demonstrate this, try the following:
>>> users = User.objects.all()
>>> print id(users[0])
58753692
>>> print id(users[0])
58753111
You can see that the object id is different each time. The object that you save is not the same object that you set the password for. The new password has not been saved to the database, so the authentication fails.
To get around this, you can use assign users[0] to a variable, or user list()
to force the queryset to be evaluated.
# Assign users[0] to a variable.
users = User.objects.all()
user = users[0]
user.set_password('whatever')
user.save()
# Force the queryset to be evaluated by using list()
users = list(User.objects.all())
users[0].set_password('whatever')
users[0].save()
Edit: This suggestion was not the problem here, but might help somebody else out.
what is the value of AUTHENTICATION_BACKENDS
in your settings file. You don't have to define it in your settings.py
, as it will default to:
AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', )
If you have set it to something else, that could cause problems.
Upvotes: 4
Reputation: 12031
Did you check that the use is active ? (User.is_active)
An inactive user has no permission and can't login (unless you customize things)
Upvotes: 1