Let’s look at how to create a user for Django using Django’s interactive shell? Make sure you have the virtual environment activated, and you are within the folder that has the manage.py file.
Note: Refer to the article How to Create a Basic Project using MVT in Django? to understand how to create a project in Django.
Let’s launch Django’s interactive shell using the below command
python manage.py shell
You will get a similar output as mentioned below
Now enter the following code in the shell to create a user for Django.
Python3
from django.contrib.auth.models import User user = User.objects.create_user( 'sonu' , 'sonu@xyz.com' , 'sn@pswrd' ) user.save() |
Let’s check our new user using superuser credentials. You can follow How to create superuser in Django? to create a superuser. Let’s start the server and log in using the admin URL (make sure path(‘admin/’, admin.site.urls), is mentioned in the URL patterns).
http://127.0.0.1:8000/admin/
Sharing the screenshot below
You can check the newly added user details under the Users section.