Class-Based Views in Django

Taranjeet Singh
May 25, 2019 · 4 min read

Creating CRUD interfaces with DRY patterns in Django.

In this tutorial, we will learn how to use Class-based views to create CRUD interfaces for any model.

At the end of this tutorial, we will be able to add, list, view, update and delete a particular model(here Book and the project is based on library). This tutorial can be extended to create CRUD based interfaces for any model.

Gif showcasing CRUD interfaces for Book model

Django has 5 class based views

  • ListView — to view the list of objects
  • CreateView — to create a particular object
  • DetailView — to view a particular object
  • UpdateView — to update a particular object
  • DeleteView — to delete a particular object

Let’s consider that we are creating a library project. We will have a Book model in an app called store.

We will create a Django application, which will use Class based Views to create CRUD interfaces around the Book model. The complete code for the repo can be found here

We will start by creating a list of all the Book objects stored in the database.

We will use ListView to create a new view called BookListView which will also have support for pagination.

In the above code, we can see that we are overriding get_context_data to add support for pagination, by passing in an additional query parameter called page.

We will add a URL for BookListView in store/urls.py

We need to make sure that the store app URLs are included in the project URLs. If not, we will have to include it.

We will now write the template code. We are using Bootstrap v4 and have a base template called __base.html from which we extend every other template. The __base.html looks like

We will now create book/list.html .

We can see that book/list.html includes a generic template _pagination.html.

With this, our first Class-Based View ListView is complete.

Listview for Books

Next, we will add support for creating a new Book by using CreateView.

We will add the following code in the views.

We will add a new URL for the above view.

We will now add a template book/create.html.

This completes our second Class based View CreateView. We can see that Class-Based Views helps us achieve DRY code pattern.

CreateView for Book

We will now create a detail view using DetailView class-based view. We will add the following code in views.

We will add the following URL to include the above detail view.

We will now add a new template book/detail.html

This completes our detail view.

DetailView for Book

Now we will add a UpdateView for this.

We will add the following code in the views.

We will add a URL for this view.

We will add book/update.html template.

This completes our UpdateView .

UpdateView for Book

We will now add the last DeleteView .

We will add the following code in the views.

We will add a URL for the above view

We will add a template book/delete.html for the above view

This completes our DeleteView.

DeleteView for Book

In this tutorial, we learned how to use Django Class-Based Views to create CRUD interfaces for a given model. This tutorial touched on the basics and focussed very little on styling and extensibility.

All About Django

Everything related to Django

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store