I am using Django (version 1.3) and have forgotten both admin username and password. How to reset both?
And is it possible to make a normal user into admin, and then
In case you do not know the usernames as created here. You can get the users as described by @FallenAngel above.
python manage.py shell
from django.contrib.auth.models import User
usrs = User.objects.filter(is_superuser=True)
#identify the user
your_user = usrs.filter(username="yourusername")[0]
#youruser = usrs.get(username="yourusername")
#then set the password
However in the event that you created your independent user model. A simple case is when you want to use email as a username instead of the default user name. In which case your user model lives somewhere such as your_accounts_app.models then the above solution wont work. In this case you can instead use the get_user_model method
from django.contrib.auth import get_user_model
super_users = get_user_model().objects.filter(is_superuser=True)
#proceed to get identify your user
# and set their user password
python manage.py changepassword username
Another way to get the user name (and most of the information) is to access the database directly and read the info from the tables.
One of the best ways to retrieve the username and password is to view and update them. The User Model provides a perfect way to do so.
Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git.
Retrieve the Python shell using the command "python manage.py shell".
Import the User Model by typing the following command "from django.contrib.auth.models import User"
Get all the users by typing the following command "users = User.objects.all()"
Print a list of the users For Python 2 users use the command "print users" For Python 3 users use the command "print(users)" The first user is usually the admin.
Select the user you wish to change their password e.g.
"user = users[0]"
Set the password
user.set_password('name_of_the_new_password_for_user_selected')
Save the new password
"user.save()"
Start the server and log in using the username and the updated password.
Another thing that is worth noting is to set your user's status is_staff
as active. At least, that's what makes it works for me. For more detail, I created another superuser
as people explained above. Then I go to the database table auth_user
and search for that username to make sure its is_staff
flag is set to 1
. That finally allowed me to log into admin
site.
use
python manage.py dumpdata
then look at the end you will find the user name