How to reset Django admin password

Gareth D.

The Problem

You’ve forgotten your password and/or username for the Django admin panel. How can you get access to the admin panel again?

The Solution

The easiest way to reset your password is by using the Django manage.py script, which includes a changepassword command. For example, if you know that the username for the admin account is admin, you can reset it by running the following code:

python3 manage.py changepassword admin

You’ll be prompted for a new password and after entering it twice, you’ll be able to log into the admin panel through the UI using that password.

Changing password for user '<admin@example.com>' Password: Password (again): Password changed successfully for user '<admin@example.com>'

Alternative options

There are a few other ways of resetting a password or finding a lost admin username that may be easier for you depending on your exact setup. You can reset a password at different levels of the Django application, including:

  • Using the Django admin panel (though only if you have access to another admin account).
  • Using the command line interface through the manage.py script.
  • Directly in the underlying database.

Generally, it’s safest and easiest to use the UI if you can and the command line script if necessary. You should avoid editing the database directly unless you know exactly what you are doing and have no other option, as it’s easy to break or corrupt your database by running database queries directly.

Using the admin panel

If you have access to another Django admin account, you can see all usernames and reset the password for the admin account you’ve forgotten by going to the “Users” page and using the reset password form.

Alternatively, you can promote another user account that you do know the credentials for to “Superuser status”.

Django admin

Using the manage.py script

You can use the changepassword command as demonstrated earlier. If you can’t use remember the username either, you can create a new admin account using the createsuperuser command. Run:

python3 manage.py createsuperuser

You will be prompted for a new username and password for a new admin account. You can then use the new admin account to find the username and/or reset the password of the existing account through the admin panel, as above.

Using the database directly

If you just want to be reminded of your admin account’s username and you can access the database directly, run:

SELECT username, email from users_customuser WHERE is_superuser;

This will return a list of all existing administrators. Your users might be stored in a different table, in which case you can run one of the following commands (depending on which database you’re using), to see all tables, and then substitute the correct table name in the above query:

\dt; # postgres .tables # sqlite SHOW TABLES; # mysql

Promoting a user to admin in the database

NOTE: this is a potentially destructive operation. Proceed with caution.

If you want to promote a normal user to admin and you only have access to the database, you can run a query similar to the following. Note that updating the database can lead to problems and you should not do this unless you have exhausted all the above alternatives. Substitute the username you want to promote at the end, and, if necessary, the table name as described above.

UPDATE users_customuser SET is_superuser=true, is_staff=true WHERE username='admin';

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.