Django Class-Based Views (CBVs) are Python classes that handle web requests and return web responses. They provide a more organized and reusable way to define logic for a specific URL.
Features of CBVs:
- Separation of Logic: Each HTTP method (like GET or POST) is handled in its own method, keeping the code organized.
- Reusable Components: Common functionality can be written once in base classes or mixins and reused in multiple views using object-oriented techniques like multiple inheritance.
- Built-in Generic Views: Django provides ready-made CBVs like CreateView, ListView, and DetailView for common tasks, saving time and effort.
Using Class based view in Django
Let's Create a Class-based view list view to display instances of a model and a model of which we will be creating instances through our view.
In geeks/models.py:
Python
from django.views.generic.list import ListView
from .models import GeeksModel
class GeeksList(ListView):
# specify the model for list view
model = GeeksModel
Now, create a URL path to map the view. In geeks/urls.py:
Python
from django.urls import path
# importing views from views..py
from .views import GeeksList
urlpatterns = [
path('', GeeksList.as_view()),
]
Create a template in templates/geeks/geeksmodel_list.html:
HTML
<ul>
<!-- Iterate over object_list -->
{% for object in object_list %}
<!-- Display Objects -->
<li>{{ object.title }}</li>
<li>{{ object.description }}</li>
<hr/>
<!-- If objet_list is empty -->
{% empty %}
<li>No objects yet.</li>
{% endfor %}
</ul>
Visit development server: http://localhost:8000/
Similarly, class based views can be implemented with logics for create, update, retrieve and delete views.
Django Class Based Views - CRUD Operations
Django Class-Based Views (CBVs) make it easier to implement CRUD operations (Create, Read, Update, Delete) by providing built-in generic views. These views save time by handling common patterns with minimal code.

- CreateView: create or add new entries in a table in the database.
- Retrieve Views: read, retrieve, search, or view existing entries as a list(ListView) or retrieve a particular entry in detail (DetailView)
- UpdateView: update or edit existing entries in a table in the database
- DeleteView: delete, deactivate, or remove existing entries in a table in the database
- FormView: render a form to template and handle data entered by user
These operations power common features like user management, blog posts, and product catalogs, making Class-Based Views a powerful and reusable way to build dynamic web applications
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice
My Profile