Modern websites often implement web-based authentication at the back-end. Such authentications are managed through the
Django comes with a built-in admin interface that can be used for user and group management.
A superuser is the admin user. Through the superuser, we can manage the entire website or application. Running the following command from inside your virtual environment creates a superuser:
python app.py createsuperuser
Following this command, you will be prompted to enter a username and password for the superuser. After confirming your password, the superuser will be created.
Superuser created successfully!
Users represent the different types of people that interact with your site.
These users might not have the same privileges as the admin, who has complete access to the website.
Through the admin/superuser, we can achieve the following:
We can create users by using the
create_user() helper function, as follows:
from django.contrib.auth.models import User user = User.objects.create_user('Tony', 'email@example.com', 'iamironman') # The user has been successfully created # We can change its attributes user.last_name = 'Stark' user.save()
We can also use the admin index page to edit user status. For a
When a user is created, they have no permissions. These permissions are granted by the superuser and are user-specific.
These permissions can be added using the helper functions:
user_permissions.add(permission, permission, ...)
user_permissions.remove(permission, permission, ...)
We can also add user permissions using the admin index. Simply click on a user, look for the User Permissions option, and use the drop-down list to add/remove permissions.
Groups are a means of categorizing users. This allows for granting permissions to a specific group. It also saves a lot of time, as compared to granting permissions to each individual user.
A user may belong to any number of groups and automatically has all the permissions granted to that group.
We create groups in a similar manner to users. Simply navigate to the Add button next to the Groups, enter the credentials, and save.
We can also add users to the Group using the admin index.
Think of group permissions as a way of assigning user permissions in bulk.
We assign permissions to a specific group, for example, by allowing Editors to edit the home screen and then adding users to the Editors group. This is a much more efficient and practical way of assigning user permissions.
View all Courses