In this Django tutorial, we will learn How to create an admin user in Django and How to reset the password for the admin user. Moreover, we will also cover the following topics in this tutorial.
- How to create Admin user in Django
- Python Django get admin password
- How to reset Admin password in Django
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.
And 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.
In this article, we will understand How to create a new admin user in Django.
How to 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, 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.
Once the admin user has been created successfully then, 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.
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. Now, we can access admin usernames in Django. For this first, we open the python shell. Here is the command to open the shell.
python manage.py shell
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
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.
Here is an example of the given execution.
In the above example, admin is a username. So, once you got your username, you can move to the next section to reset your admin password.
Also Read: How to setup Django project
How to 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.
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.
In the example, we have successfully reset the password for username “admin“.
You may also like reading the following articles.
- Python Django vs Flask
- Python Django random number
- How to get user IP address in Django
- How to Get Current time in Django
- Difference between app and project in Django
- Create model in Django
- Python Django filter
So, in this tutorial, we discussed how to create an admin user in Django and we have also covered the following topics.
- How to create admin user in Django
- Python Django get admin password
- How to reset admin password in Django
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.