Django Authentication: Login and Logout 🎯

beginner
21 min

Django Authentication: Login and Logout 🎯

Welcome to our comprehensive guide on Django Authentication! In this tutorial, we'll walk you through creating a login and logout system using Django, a powerful Python web framework. By the end of this lesson, you'll have a solid understanding of authentication in Django and be able to apply these skills to your own projects. πŸ’‘ Pro Tip: This tutorial is suitable for both beginners and intermediates, so let's get started!

Table of Contents

  1. Prerequisites
  2. Setting up the Project
  3. Creating User Models
  4. Defining URLs for Authentication
  5. Creating Login and Logout Views
  6. Testing the Authentication System
  7. Quiz: Test Your Knowledge

<a name="prerequisites"></a>Prerequisites πŸ“

Before diving into authentication, ensure you have the following prerequisites:

  • Python 3.8 or higher installed on your system
  • Basic understanding of Python programming
  • Familiarity with the Django web framework

<a name="setting-up-the-project"></a>Setting up the Project πŸ“

Begin by creating a new Django project:

bash
django-admin startproject mysite cd mysite

Create a new app within the project:

bash
python manage.py startapp auth

Next, add the newly created app to the INSTALLED_APPS list in the project's settings.py file:

python
INSTALLED_APPS = [ ... 'auth', ]

Now, let's configure the authentication system.

<a name="creating-user-models"></a>Creating User Models πŸ“

By default, Django comes with a built-in User model, but for our purposes, we'll create a custom User model. First, create a new file called models.py in the auth app directory and add the following code:

python
from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): pass

After creating the CustomUser model, we need to update the AUTH_USER_MODEL setting in the project's settings.py file:

python
AUTH_USER_MODEL = 'auth.CustomUser'

Now, let's define the URLs for authentication.

<a name="defining-urls-for-authentication"></a>Defining URLs for Authentication πŸ“

Next, create a new file called urls.py in the auth app directory and add the following code:

python
from django.urls import path from django.contrib.auth import views as auth_views urlpatterns = [ path('login/', auth_views.LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), ]

Now that we've defined the URLs, let's create the views for login and logout.

<a name="creating-login-and-logout-views"></a>Creating Login and Logout Views πŸ“

To include these views in our project, we'll first need to update the project's urls.py file:

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

Now, let's create templates for the login and logout pages.

<a name="creating-templates"></a>Creating Templates πŸ“

Create a new folder called templates within the auth app directory and add two subfolders: login and logout. Inside each folder, create a new file called base.html. The content for these files is beyond the scope of this tutorial, but you can find examples online.

<a name="testing-the-authentication-system"></a>Testing the Authentication System πŸ’‘ Pro Tip:

To test the login and logout functionality, run the development server and navigate to the following URLs in your web browser:

  • http://127.0.0.1:8000/auth/login/ (for login)
  • http://127.0.0.1:8000/auth/logout/ (for logout)

Now that you've created a login and logout system in Django, you can build upon these foundations to create more sophisticated authentication features, such as registration, password reset, and user management.

<a name="quiz"></a>Quiz: Test Your Knowledge 🎯

Quick Quiz
Question 1 of 1

Which of the following is the name of the built-in User model in Django?


That's it for our Django Authentication tutorial! We hope you found this lesson helpful and informative. As you continue to learn and practice Django, you'll find that the authentication system is a vital component of any web application, and understanding how it works will enable you to build secure and scalable projects. Good luck, and happy coding! πŸš€

Back to CodeYourCraft