How to Reset Admin Password in Django

Salman Mehmood Feb 02, 2024
How to Reset Admin Password in Django

This short article will explain the admin panel, look at how to change an admin password, and see how we can modify the admin user name in Django.

Reset the Admin Password in Django

When we launch our Django project, Django set us up with an SQLite database, the default configuration. There are two ways to push some data into the database.

One by creating a page where a user can push data, or we can have an admin page. Normally admin page is accessed by admins or the superusers.

The admin or superuser will be able to make changes and control the entire project.

Let’s see how to change the admin password in Django. We first need to open the terminal in the manage.py file and run the following command.

python manage.py changepassword admin

Now we can see that we have changed the admin password successfully.

changed the admin password successfully

We need to run the Django server and go to the admin panel through our browser.

python manage.py runserver

Let’s try to put the old password to open the admin panel.

put the old password

We can see that we cannot access the admin panel using the old password. But when we go through the modified password, we can access the admin panel we created new, and the admin user is marked with a green check.

modified password

Let’s see how to change the admin user name in Django. We first need to open the terminal in the manage.py file and run the following command.

python manage.py shell

We need to import the User class into the interactive shell.

from django.contrib.auth.models import User

We need to initialize an object to get the existing admin user name.

ChangeUserName = User.objects.get(username="admin")

We need to set a new user name using this code.

ChangeUserName.username = "john"

We will save the new user name in the backend using the save() method.

ChangeUserName.save()

We can now access the admin panel through the modified user name. Let’s run the server and try to access the admin panel using a modified user name.

modified user name

We can see that we have modified the user name successfully while following the simple steps.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Django Password