Django

Implement CRUD Operations Using Django Class-Based Views

🎯 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 data
  • CreateView → insert data
  • UpdateView → modify data
  • DeleteView → remove data
  • reverse_lazy() is used for redirection

🔥 Viva Questions

  1. What are Class-Based Views?
  2. Difference between CBVs and FBVs?
  3. What is reverse_lazy()?
  4. Why use generic views?
  5. Which view is used for delete operation?

Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *