Django, a Python web application framework, provides a built-in authentication and authorization system that allows developers to build secure and trusted web applications. This chapter will go into detail about this system and how it can be customized to meet the needs of different projects.

Authentication

Authentication is the process of verifying a user's identity. In other words, it's how the system determines who the user is. In Django, authentication is performed using the built-in authentication system.

Django's authentication system provides a User model that represents the system's users. This template has fields to store information such as username, password, email, first and last name. In addition, the User model has methods to verify the password, change the password, and verify that the user account is active.

To authenticate a user, Django provides the authenticate() function. This function accepts a username and password and returns a User object if authentication is successful. Otherwise, it returns None.

Authentication example

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
    # A backend authenticated the credentials
else:
    # No backend authenticated the credentials

Authorization

Authorization is the process of determining what an authenticated user is allowed to do. In Django, authorization is performed using the built-in permissions system.

Django's permissions system provides a way to define what users can and cannot do. Permissions are defined in terms of templates and actions. For example, you can set a permission that allows a user to add, change, or delete instances of a specific model.

Permissions are stored in the Permission template, which has fields for the name of the permission, the content of the permission type, and the template to which the permission applies. The User model has a many-to-many relationship with the Permission model, which means that a user can have multiple permissions, and a permission can be assigned to multiple users.

Example authorization

from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from myapp.models import BlogPost

content_type = ContentType.objects.get_for_model(BlogPost)
permission = Permission.objects.create(
    codename='can_publish',
    name='Can Publish Posts',
    content_type=content_type,
)
user = User.objects.get(username='john')
user.user_permissions.add(permission)

In addition, Django provides the has_perm() function that checks whether a user has a specific permission. This function accepts the name of the permission and returns True if the user has the permission and False otherwise.

Example permission check

if user.has_perm('myapp.can_publish'):
    # The user has the permission
else:
    # The user does not have the permission

Conclusion

Django's authentication and authorization system is a powerful tool that allows developers to build secure and reliable web applications. With it, you can control who has access to your app and what they can do. And thanks to Django's flexibility, you can customize this system to suit your project's needs.

Now answer the exercise about the content:

What is the role of Django's built-in authentication and authorization system?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Models in Django: Tests in Django

Next page of the Free Ebook:

88Models in Django: Tests in Django

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text