Sentry Answers>Django>

How to reset Django admin password

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:

Click to Copy
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.

Click to Copy
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:

Click to Copy
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:

Click to Copy
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:

Click to Copy
\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.

Click to Copy
UPDATE users_customuser SET is_superuser=true, is_staff=true WHERE username='admin';
  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • ResourcesBackend Error Monitoring 101
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,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.

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