This video tries to illustrate the usage of default authentication system already built in the Django.
Django provides the in-built auth app to handle the process of authentication and authorisation. Authentication is a process in which Django tries to confirm the identity of a user by checking if the user has the correct credentials to login to the system. Authorisation is the process that checks which part of the application user has the access and which part is denied for that user.
After creating a project in Django, creating an app and running the migration, the related tables for authentication and authorisation will be created in the database. The table auth_user saves the username and the encrypted password. The other tables like auth_group and auth_permission and related tables are helpful in handling the authorisation process. You may import useful auth-related functions from the package django.contrib.auth:
from django.contrib.auth import authenticate, login, logout
You can use the variable {{ request.user }} in the template to get the value for the username. The function request.user.is_authenticated can be used both in the template and in the view to determine if the user is authenticated.
login_required is the decorator that can be used on the view functions to restrict the views access to the authenticated users only. This decorator look for the constant LOGIN_URL in the settings file. This constant will help in redirecting the user to the concerned page if the user is not authenticated.
LOGIN_URL = 'login'
Here, the user will be redirected to the login page.