How to create user and superuser in Django Authentication System?


The demand for dynamic websites increased as more features were required in websites. In this, in addition to delivering content, the server stores data and responds to user input. One of the key reasons for having a dynamic site is the ability to authenticate users and limit the material they may access.

User objects are the core of the authentication system. Like mentioned above, users are the people interacting with your site. they are used to enable things like restricting access, registering user profiles, associating content with creators etc.

Let us go ahead and understand how to create users and an admin user that has all permissions in the following sections.

The primary attributes to a user are username password, email, first_name and last_name.

Creation of user and superuser

To create users, the in built create_user helper function can be included.

from django.contrib.auth.models import User
user= User.objects.create_user(‘john’, johnDoe@tutorialspoint.com’,’ttspassword’)
user.last_name=’Doe
user.save()

To create a superuser the createsuperuser command is used.

Python manage.py createsuperuser  username=joe  email=johndoe@tutorialspoint.com

After this, you will be prompted to enter a password. After entering, the user will be created instantly.

Creation of a superuser is essential since it is like the admin for the application and all permissions are automatically given to it.

Whereas when a regular user is created, they can be a part of a group or be given permissions individually. For example, you are running an ecommerce site and your website has customers and employees as its users, an app named review and a model named product.

Here, the admin of the ecommerce site is neither a customer nor an employee. The admin is one, that manages the site and the permissions which automatically makes it the superuser. The customer and employee groups depict users that are using the site and are performing different roles.

Updated on: 05-Sep-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements