Python Django get admin password

In this Django tutorial, we will learn How to get an admin password in Python Django, step by step. There are many situations when we need to reset the admin password, so we will also explore the procedure to reset the password for the admin user. Moreover, we will also cover the steps of creating the admin user in Django.

Before, we deep dive into the steps of creating an admin user. Let’s first get some introduction:

You all know that Django is a powerful Python-based web development framework. When we install Django, it by default provides an administrative interface. Now, this admin interface uses the model’s metadata to provide a model-centric interface.

This interface is used to manage the content of our web application. Moreover, we can also customize this admin interface according to our website requirements. So, to access this admin interface we need to create an admin user.

Create Admin user in Django

Creating a new admin user is very easy in Django. For this, we first need to move to our project’s root directory. After this, we need to run the following command.

python manage.py createsuperuser

To create a new admin user in Django, we use the createsuperuser command, and this command can be executed using the manage.py utility.

Once we execute the given command, it will prompt some fields like Username, Email, Password, and Confirm Password. And once we will enter the data for all these fields our new admin user will be created.

Note:- If you are using this command just after creating the project then make sure you run the “python manage.py migrate” command first. And then, use the createsuperuser command.

Here is an example of the above implementation.

How to create Admin user in Django
Example

Once the admin user has been created successfully, we can use these credentials to access the admin interface. For this, we need to start the development server and open the “http://127.0.0.1:8000/admin/” address in the browser.

This will open the Django administration login page. Now, we simply need to use the credentials to log in as an admin user.

How to login Admin user in Django
Login Page

Read: How to install Django

Python Django get admin password

Now, while working on your Django project, you might be stuck in a situation where you can’t remember your admin credentials. So, in this section, we will try to understand How we can get the admin password.

Django uses its own authentication system for the admin interface and it is one of the secure frameworks. We cannot directly access the admin password. One of the standard solutions to overcome this issue is to either reset the admin password or create a new admin user.

Now, if you remember your username then we can easily change the admin password. This situation is explained in the next section.

But, what if we also don’t remember the username? For this, we can also access admin usernames in Django by following the steps given below.

Step-1: Access Django Shell

For this first, we open the Python shell. Here is the command to open the shell.

python manage.py shell

Step-2: Import Necessary Modules

This will open the interactive Python console. Next, we need to import the User model from Django. For this, we can use the following command.

from django.contrib.auth.models import User

Step-3: Retrieve Django Users

Once the model has been imported, we have to use this model to get the all users. For this, we will use the following code.

users = User.objects.all()

Now, we can easily get the usernames by just printing the users variable that we just created.

print(users)

Here is an example of the given execution.

Python Django get admin password
Example

In the above example, admin is a username. So, once you get your username, you can move to the next section to reset your admin password.

Also Read: How to setup Django project

Reset Admin password in Django

Now, in this section, we will learn How we can reset the admin password if we know the username of our admin.

Method: By using mange.py file

For this, we simply need to move to the project’s root directory where the manage.py file is located. And then, we have to use the following syntax.

python manage.py changepassword <username>

In the above syntax, we are using the manage.py utility to change the password for the given username. In the syntax, we have to give the username of the admin in place of <username>. After this, it will ask for the new password that we want to set for the given username.

For better understanding, consider the following example shown in the given image.

How to reset Admin password in Django
Example

In the example, we have successfully reset the password for the username “admin“.

Conclusion:

In conclusion, we have learned how to retrieve the admin password in Python Django. We have also learned the procedure of creating the admin user in Django with the process of resting the password of the admin user as it is a good practice to change the password from time to time for security purposes.

You may also like reading the following articles.