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.
[tags]django,python,commandline,auth,login,password[/tags]
Related posts:
- Howto use Satchmo as an App I'm going to be using Satchmo to power the backend...
- Helpful Django utilities and links Two simple links which have been very helpful in doing...
- Speedy Django Development As I’ve mentioned before, I am building my own web...
- Top ten reasons to use Django for your web framework Django is pulling ahead of every other web framework I’ve...
- First use of the Django Magic Removal Last night I took a break from my most recent...
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()
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.
Thank you
Thanks! Worked perfectly.
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.
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.)
Cudo’s!!!!
Is this useful or what! I mean am I stupid or what ..
Regards,
Gerard
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.
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
I confirm this worked for me with Pinax (using Django 1.3) while the original did not work. Thanks
rorys, thanks for your contribution, it worked for me. thanks bruce too
Rorys example worked for me. Thank you
Rorys,
Likewise for me (and apparently Amit). I’m using the latest development Django (as of 2010-08-10), fully updated Ubuntu distribution of Python 2.6 (r265:79063, Apr 16 2010, 13:09:56), and development version of Pinax (0.9…?). Perhaps something to do with Python 2.6 object array dereferencing with brackets?
thank you very much! it’s helpful!
Using Django 1.1.1 I had to use Amit Chakradeo’s/rorys’ way of doing it, but that worked like a charm.
I was too quick…
Using the method Amit/rorys described _did_ change the password in django.contrib.auth.models.User but it _did_not_ let me log into the development server
. Neither could I log in with a new superuser created according to the instructions in the tutorial.
I’ll investigate it and report any solution I find.
I still don’t know what caused the problem, but changing the browser helps. In Google Chrome (5.0.342.7 on Mac OS X 10.6.3) I cannot log in when I get to the second part of the Django tutorial and the terminal outputs a fairly long report ending with “error: (32, ‘Broken pipe’)”.
However, changing to Firefox (3.6.3) let’s me log in. (I was tipped off by this post: http://code.djangoproject.com/ticket/4444.)
I still don’t know what the problem is but a work-around is good enough at the moment and maybe it can be helpful to someone else too.
The method from the comments worked for me. You might want to remove that semi-colon, since it betrays a background with some less cool language.
.-= Owen Byrne´s last blog ..Top Digg Unpromoted Stories 2009-12-05 to 2009-12-12 =-.
you can try to google Password Genius
(4+ years later – still this post is found at google and helps the occasional person that forgets the obvious…)
Anyway, was curious why Amit’s reply worked (while the orignal didn’t).
So it seems that he important difference is that Amit does
user = …
user.set_password
user.save
instead of the original post which was working on the full path
users[0].set_password(‘whatever’)
users.save()
After failing to change the password in that way I just rewrote it as
user = users[0]
user.set_password(‘whatever’)
user.save()
and that worked perfectly.
diomedes
I think understand why:
users[0].set_password(‘whatever’)
doesn’t work, rory’s solution does.
It is because ‘users’ is a queryset, which essentially means that “users[0]” is better thought of as the execution of a function instead of an “object” that is storing information about the user. When users[0] is called, it executes a database query and returns the result. In other words, “users[0].set_password(‘whatever’)” returns the information of the user with the modified password, but this information is *not* saved in users[0], since a queryset can’t actually hold the result of the database query.
~/my/app$ ./manage.py changepassword admin
from: https://docs.djangoproject.com/en/dev/ref/django-admin/#changepassword
Me thinks this is the best way to do it now.
It make sense to change the password via a configuration setting prior to a syncdb specially if you are on the production server. You don’t want to jump into typing python code directly.
Check django-bootup for more details.
https://github.com/un33k/django-bootup/blob/master/README
Why not just do it from the manager?
./manage.py changepassword admin
Posters who are pointing out that you can now do that from a simple “changepassword” command are right! When I wrote this, there was no such command. Now it is much easier.