Howto Reset The Admin Password in Django

Posted on | February 16, 2007 | 14 Comments

I keep needing to do this, darn it! I leave a project for a few months, possibly push it live, and then I go back to fire up the dev server but I simply cannot remember the password I used for admin during development.

Why don’t I use my stupid-development-password? I don’t know, but this seems to happen with some regularity.

Luckily, it is trivial to fix with a few lines at the python commandline:

Deep:/opt/webapps/invisible bruce$ ./manage.py shell    
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
Type "copyright", "credits" or "license" for more information.

IPython 0.7.2 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from django.contrib.auth.models import User

In [2]: users = User.objects.all()

In [3]: users
Out[3]: [<User: admin>]

In [4]: users[0].set_password('whatever');

In [5]: users[0].save()

That’s it, fire up the dev server again and your new password will get you in.

Technorati Tags: , , , , ,

Comments

14 Responses to “Howto Reset The Admin Password in Django”

  1. Amit Chakradeo
    April 9th, 2007 @ 9:27 am

    This infact did not work for me as is. But if I get the user object by explicit username, it worked. Maybe this is a bug. I will need to do some testing and report it…

    So your example did not work, but the following worked:
    u=User.objects.get(username__exact=’admin’)
    u.set_password(‘whatever’);
    u.save()

  2. links for 2007-07-09 « PaxoBlog
    July 10th, 2007 @ 7:19 am

    [...] Howto Reset The Admin Password in Django In [1]: from django.contrib.auth.models import User In [2]: users = User.objects.all() In [3]: users Out[3]: [] In [4]: users[0].set_password(’whatever’); In [5]: users[0].save() (tags: django password recovery login auth commandline python) [...]

  3. Chris
    August 8th, 2007 @ 3:48 pm

    Thank you for posting such useful information! That was easy and it worked beautifully with Django 0.96.

    I was just about ready to bust out sha1sum and replace the hash in the MySQL database, but this was much better.

  4. Antony C
    August 14th, 2007 @ 7:40 pm

    Thank you :)

  5. Ben Chapman
    February 14th, 2008 @ 7:20 am

    Thanks! Worked perfectly.

  6. Unixmonkey
    April 18th, 2008 @ 9:08 am

    Saved me the hassle of firing up another django app, setting the admin password, and copying over the sha1 hash from one database to another.

    Thanks a lot.

  7. Ed W
    May 23rd, 2008 @ 4:25 pm

    You are a godsend! That worked and it saved my bacon.
    Well, at least it saved me from having to painfully re-install
    the app. (Installing the app was PAINFUL.)

  8. Gerard
    August 15th, 2008 @ 7:07 am

    Cudo’s!!!!

    Is this useful or what! I mean am I stupid or what .. ;-)

    Regards,

    Gerard

  9. trailblazer
    October 24th, 2008 @ 10:26 pm

    thanks a lot. I’m new to django. I’ve created site set some silly password and forgot. I didn’t even remember I’ve created something like super user. It was only when I read from second tutorial on django site that I came to know about it.

  10. rorys
    July 2nd, 2009 @ 8:48 am

    This didn’t work for me as described above, but did work if I did the following:

    >>> from django.contrib.auth.models import User
    >>> users = User.objects.all()
    >>> u = users[0]
    >>> u.set_password(‘whatever’)
    >>> u.save()

    Perhaps this is an issue with the newest version of Django? Not sure. but hopefully that will be helpful to someone else.

    thx

  11. Rafa
    August 24th, 2009 @ 1:31 am

    rorys, thanks for your contribution, it worked for me. thanks bruce too :)

  12. Pascal
    December 2nd, 2009 @ 8:02 am

    Rorys example worked for me. Thank you

  13. billschen
    December 21st, 2009 @ 11:41 pm

    thank you very much! it’s helpful!

  14. Ehemminger Dot Org » Blog Archive » links for 2010-02-28
    March 1st, 2010 @ 12:15 am

    [...] Howto Reset The Admin Password in Django | Coder's Eye (tags: django) [...]

Leave a Reply





CommentLuv Enabled

Video & Audio Comments are proudly powered by Riffly