One of the key features of Django is its built-in support for creating projects on top of CRUD (Create, Read, Update, Delete) operations. While Django’s class-based views provide a fast, easy, and flexible way to build web applications, many developers still use function-based views.

Class-based views offer several benefits over function-based views, including inheritance, code structuring, code reusability, and more. While implementing class-based views may appear slightly complex, this guide will assist you in comprehending the concept by building a task manager app and providing step-by-step instructions.

a list of tasks in a task manager app

What Are Class-Based Views in Django?

In Django, views arePython functionsthat take a web request and return a web response. Class-based views (CBVs) are an alternative way to define views in Django using Python classes instead of functions.

CBVs have several advantages, such as better code organization, easier reuse of code, and the ability to use inheritance to create variations of existing views. CBVs also provide built-in methods such as theget()andpost()methods that you’re able to overwrite for custom behaviors.

full details of a task to buy groceries

The code used in this article is available in thisGitHub repository.

Class-Based Views Available in Django

Django provides some built-in CBVs for popular use cases, such as displaying lists of objects or creating new ones. Some of these built-in CBVs are:

Django also provides other views, includingTemplateView,RedirectView, andFormView. you may refer toDjango’s documentationfor detailed information on class-based views.

a form to create and edit tasks

Build a Task Manager App With Django Class-Based Views

Building an app such as a task manager app will let you understand how to implement CRUD operations with CBVs. A task manager has features that allow users to create, update, delete, and read tasks. These features are in line with CRUD operations. The following steps will help you build a task manager app with Django CBVs.

Set Up a Django Project

To create a task manager app with Django, you should start by following these steps:

Create a Model for Your Task Manager App

In your app directory (ortask_managerfolder), open yourmodels.pyfile and create a model for your task manager app. Here’s a sample model you can use:

Migrate your model with this command:

Create a Django Form for Your App

You should have a form for handlingCreateandUpdateoperations. In your app directory, create a file calledforms.py. Here’s an example:

The code above has a class calledTaskFormwhich defines the fields and widgets of the form. It also specifies the model to use.

a page to confirm the user’s delete operation

Create Django Views for Each CRUD Operation

A basic CRUD app with CBVs requires at least four views to handle all operations effectively. The next few steps will show you how to create them.

Import the Necessary Modules and Packages

Open yourviews.pyfile and make the following imports:

The code above imports five CBVs. It also importsreverse_lazyto redirect the user to a specified URL after a form submission. Finally, it imports theTaskmodel, and theTaskFormcreated earlier.

Create a View to List Model Objects

A task manager app should have a page listing all the tasks created by the user. To create a view for this, you should use theListView. Here’s an example:

The view above defines three attributes which are:

Most CBVs will contain these three attributes.

Create a View to Handle Task Details

Each task a user creates should have a page showing its details. The ideal CBV to handle this isDetailVew. Here’s a simple example:

Create a View for Task Creation

Create a view to handle the creation or addition of new tasks. This is theCreatepart of the CRUD operations, and the right view for this is theCreateView. Here’s how to use it:

The code above introduces two new attributes:form_classandsuccess_url.

Theform_classattribute tells the view which form class to render and use for its operations.

Thesuccess_urlspecifies how to redirect the user after submitting the form. It uses thereverse_lazyfunction which takes the name of a URL path.

Create a View for Editing Tasks

To allow your users to edit or update their tasks, you should create a view that looks like this:

The above view is similar to theTaskCreateViewcreated earlier. The only difference is the use of theUpdateView.

Create a View to Handle Delete Operations

To allow your users to delete tasks whenever they want, you should use theDeleteViewCBV. Here’s an example:

Configure Your App’s URLs

In your app directory, create aurls.pyfile and configure your URL patterns like this:

The URL patterns above are similar to URLs created with function-based views. The difference is theas_view()function appended to the end of each view name.

You canuse Django slugs to create URLsinstead of the primary key used above.

Create Templates for Your Views

Allowing your users to perform the actions defined in the views above suggests that you provide an interface for them to interact with. From the views created earlier, the task manager app should have four user interfaces.

In your app directory, create four HTML templates. You should also createbase.htmlfile. You canstyle your Django templates with Bootstrapto save time.

Task List Template

This template should include code that lists all the tasks in the model. A skeletal example of the code is this:

With some Bootstrap classes, you can make your page look like this:

Task Detail Template

This page should show the full details of each task created. Here’s an example template you may use:

Depending on your styling approach, your page should look like this:

Task Form Template

This template should contain a form that lets the user create or update a task.

The template will look like this:

Delete Task Template

This template should be a confirmation page to prevent accidental deletion of tasks.

With some Bootstrap, your page should look like this:

Use Class-Based Views to Increase Your Productivity

Class-based views are a great way to write clean, organized code in a short amount of time, thereby increasing your productivity. You should use them in your projects as much as possible. Also, you’re able to further integrate features like search functionality, notifications, and so on to make your task manager app a fully-fledged functional application.