🎯 Objective
To implement Create, Read, Update, Delete (CRUD) operations using Django Class-Based Views (CBVs) for better structure and code reusability.
📌 What are Class-Based Views?
Class-Based Views (CBVs) are an alternative to function-based views. They provide:
- Reusable code
- Less duplication
- Built-in functionality for CRUD
📦 Step 1: Model (Student)
# models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
course = models.CharField(max_length=100)
def __str__(self):
return self.name
🔁 Step 2: Run Migrations
python manage.py makemigrations
python manage.py migrate
🧠 Step 3: Import Generic Views
# views.py
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Student
📄 Step 4: Create Views
🔹 1. List View (Read)
class StudentListView(ListView):
model = Student
template_name = 'student_list.html'
context_object_name = 'students'
🔹 2. Create View
class StudentCreateView(CreateView):
model = Student
fields = ['name', 'age', 'course']
template_name = 'student_form.html'
success_url = reverse_lazy('student_list')
🔹 3. Update View
class StudentUpdateView(UpdateView):
model = Student
fields = ['name', 'age', 'course']
template_name = 'student_form.html'
success_url = reverse_lazy('student_list')
🔹 4. Delete View
class StudentDeleteView(DeleteView):
model = Student
template_name = 'student_confirm_delete.html'
success_url = reverse_lazy('student_list')
🌐 Step 5: URL Configuration
# urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', StudentListView.as_view(), name='student_list'),
path('add/', StudentCreateView.as_view(), name='student_add'),
path('update/<int:pk>/', StudentUpdateView.as_view(), name='student_update'),
path('delete/<int:pk>/', StudentDeleteView.as_view(), name='student_delete'),
]
🎨 Step 6: Templates
📄 student_list.html
<h2>Student List</h2>
<a href="{% url 'student_add' %}">Add Student</a>
<ul>
{% for student in students %}
<li>
{{ student.name }} - {{ student.course }}
<a href="{% url 'student_update' student.id %}">Edit</a>
<a href="{% url 'student_delete' student.id %}">Delete</a>
</li>
{% endfor %}
</ul>
📄 student_form.html
<h2>Student Form</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
📄 student_confirm_delete.html
<h2>Confirm Delete</h2>
<form method="post">
{% csrf_token %}
<p>Are you sure you want to delete?</p>
<button type="submit">Yes</button>
</form>
✅ Output
- View all students
- Add new student
- Update student
- Delete student with confirmation
🧠 Exam Tips
- CBVs reduce code duplication
ListView→ display dataCreateView→ insert dataUpdateView→ modify dataDeleteView→ remove datareverse_lazy()is used for redirection
🔥 Viva Questions
- What are Class-Based Views?
- Difference between CBVs and FBVs?
- What is
reverse_lazy()? - Why use generic views?
- Which view is used for delete operation?
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
