Resetting your django password
Today, I came back to a site that I have not been working on for over a month, and to my shock, I could not remember my password for the site. This was driving me nuts for a while, until I decided to just change it with some python code. The cool thing is that you can do this directly from your shell.
So if you ever forget your django password, here is how you can change it:
First we run our django/python shell:
python manage.py shell
Now that we have our interactive shell, we load our list of users:
>>> from django.contrib.auth.models import User >>> users = User.objects.all()
We need to make sure we change the password for the correct user, so we do,
>>> users [<User: admin>, <User: andre>]
Great, so we want to change the admin password. It's the first in our list so it will be index [0]. After we changed the password, we save the user back to the database, and exit our shell.
>>> users[0].set_password('newpass')
>>> users[0].save()
>>> exit()
That should do it :)
I hope this helps those of us silly enough to forget important passwords.
Happy Djangoing!
Willem - on 16 May 2010
Hi Zahid
Funny as I had to do this today as well. Was working crazy early hours and didn't want to wake someone up for a password. Now tonight I happened to see this post.
For some reason new versions of Django require that you do this:
After you did the: users = User.objects.all() and you found the admin username.
>>> users [<User: admin>, <User: andre>]
Then instead of using: >>> users[0].set_password('newpass') >>> users[0].save()
you first create an instance of only the admin user using something like: >>> u = users[0]
then: >>> u.set_password('newpass') >>> u.save()
But I didn't really look much into this, so it might be something else that was wrong. To be honest I don't know why it wouldn't just work doing the users[0] thing directly.
Andre - on 17 May 2010
Oh thanks for the info Willem! I will have to update my post about this as soon as I'm back from holiday.
You can use Markdown to format your comment.
zahid - on 11 May 2010
Hello,
I used the process but cannot login to my django admin. I changed password successfully.
Some other places i found that I need to go to admin.py shell (http***www.pkshiu.com/loft/archive/2008/08/resetting-django-admin-password)
I badly need to enter to the admin. Please help me
Thankyou