Django Class Based Views
Class-Based Views (CBVs) in Django are Python classes that handle web requests and send responses in a structured and reusable way.
- Each HTTP method, such as GET or POST, is handled in its own method for better organization.
- Shared features can be placed in base classes or mixins and reused through inheritance.
- Django offers built-in CBVs like CreateView, ListView, and DetailView for common tasks.
- CBVs help reduce repeated code and make view logic cleaner.
Using Class-based view in Django
Consider a project named ‘geeksforgeeks’ having an app named ‘geeks’. After setting up the project and app, create a Class-Based List View to display instances of a model, along with the model for which these instances will be created and shown through the view.
In geeks/models.py:
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:
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:
<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