Authentication in Django: Authentication Configuration in Django

Capítulo 132

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Authentication is a crucial component of any web application, and Django, a powerful Python framework for web development, provides a robust and secure authentication system. In this course, we'll explore configuring authentication in Django.

Django comes with a built-in authentication system that handles user registration, logins, logouts, and password management. To start using authentication in Django, you need to understand the concept of the 'User Model'. The User Model is a data model that Django uses to store information about your application's users. This template is highly customizable to suit your needs.

Authentication Configuration

To configure authentication in Django, you first need to add 'django.contrib.auth' and 'django.contrib.contenttypes' to your INSTALLED_APPS in your settings.py file:

INSTALLED_APPS = [
    ...
    'django.contrib.auth',
    'django.contrib.contenttypes',
    ...
]

Next, you need to set middleware authentication in your settings.py file. The middleware is a series of hooks that Django uses to process requests/responses before they reach your view or leave your view. Add 'django.contrib.auth.middleware.AuthenticationMiddleware' to your MIDDLEWARE:

MIDDLEWARE = ​​[
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
]

Django now knows you want to use authentication. But we still need to configure the URLs for login, logout and registration. Add the following lines to your urls.py file:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
]

With these settings, Django now knows where to look when a request is made to '/accounts/login/', '/accounts/logout/', etc.

Customizing Authentication

Django allows you to customize the User Model to suit your needs. Let's say you want to add a 'phone' field to the User Model. You can do this by creating a new template that inherits from 'AbstractUser' and adding the phone field:

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    phone = models.CharField(max_length=20)

Next, you need to tell Django to use this CustomUser as the User Model. Add the following line to your settings.py:

AUTH_USER_MODEL = 'myapp.CustomUser'

Where 'myapp' is the name of your Django application.

Conclusion

Authentication is a critical component of ensuring the security and integrity of user data in your application. Django offers a robust and easy-to-use authentication system that you can get started with just a few lines of code. With additional customization, you can adapt the authentication system to perfectly suit your needs.

This course will go into more detail about authentication in Django, including how to handle user permissions, groups, and how to customize the authentication form. Stay tuned for the next chapters!

Now answer the exercise about the content:

What is the User Model in Django and how can it be customized?

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

You missed! Try again.

The Django User Model is a data model used to store information about an application's users. It is highly customizable, allowing you to extend it by creating a new model that inherits from AbstractUser. For example, you can add a 'phone' field by creating a CustomUser model and configuring Django to use it by updating the AUTH_USER_MODEL setting in your settings.py file.

Next chapter

Authentication in Django: User Models in Django

Arrow Right Icon
Free Ebook cover System creation course with Python and Django complete
75%

System creation course with Python and Django complete

New course

176 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.