All the common Django class-based views (CBVs) with a full example, including:
- List View
- Detail View
- Create View
- Update View
- Delete View
- How to connect one view/template to another (like a link or redirect)
π§ͺ Example Model
Letβs assume you have a model like this:
# models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
department = models.CharField(max_length=100)
def __str__(self):
return self.nameπ§ 1. ListView β Show All Students
# views.py
from django.views.generic import ListView
from .models import Student
class StudentListView(ListView):
model = Student
template_name = 'students/student_list.html' # Optional
context_object_name = 'students'# urls.py
from django.urls import path
from .views import StudentListView
urlpatterns = [
path('students/', StudentListView.as_view(), name='student-list'),
]<!-- templates/students/student_list.html -->
<h2>All Students</h2>
<ul>
{% for student in students %}
<li>
<a href="{% url 'student-detail' student.pk %}">{{ student.name }}</a>
</li>
{% endfor %}
</ul>
<a href="{% url 'student-create' %}">Add Student</a>π 2. DetailView β Show a Single Student
# views.py
from django.views.generic import DetailView
class StudentDetailView(DetailView):
model = Student
template_name = 'students/student_detail.html'
context_object_name = 'student'# urls.py
from .views import StudentDetailView
urlpatterns += [
path('students/<int:pk>/', StudentDetailView.as_view(), name='student-detail'),
]<!-- templates/students/student_detail.html -->
<h2>{{ student.name }}</h2>
<p>Age: {{ student.age }}</p>
<p>Department: {{ student.department }}</p>
<a href="{% url 'student-update' student.pk %}">Edit</a>
<a href="{% url 'student-delete' student.pk %}">Delete</a>
<a href="{% url 'student-list' %}">Back to List</a>β 3. CreateView β Add a Student
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
class StudentCreateView(CreateView):
model = Student
fields = ['name', 'age', 'department']
template_name = 'students/student_form.html'
success_url = reverse_lazy('student-list')urlpatterns += [
path('students/create/', StudentCreateView.as_view(), name='student-create'),
]<!-- templates/students/student_form.html -->
<h2>Add Student</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
<a href="{% url 'student-list' %}">Cancel</a>βοΈ 4. UpdateView β Edit a Student
from django.views.generic.edit import UpdateView
class StudentUpdateView(UpdateView):
model = Student
fields = ['name', 'age', 'department']
template_name = 'students/student_form.html'
success_url = reverse_lazy('student-list')urlpatterns += [
path('students/<int:pk>/update/', StudentUpdateView.as_view(), name='student-update'),
]It uses the same form template as the CreateView.
β 5. DeleteView β Delete a Student
from django.views.generic.edit import DeleteView
class StudentDeleteView(DeleteView):
model = Student
template_name = 'students/student_confirm_delete.html'
success_url = reverse_lazy('student-list')urlpatterns += [
path('students/<int:pk>/delete/', StudentDeleteView.as_view(), name='student-delete'),
]<!-- templates/students/student_confirm_delete.html -->
<h2>Are you sure you want to delete {{ student.name }}?</h2>
<form method="post">
{% csrf_token %}
<button type="submit">Yes, Delete</button>
</form>
<a href="{% url 'student-detail' student.pk %}">Cancel</a>π Bonus: Linking One View to Another
In templates, use {% url %} to link to other views. For example:
<a href="{% url 'student-detail' student.pk %}">Details</a>
<a href="{% url 'student-create' %}">Add New Student</a>β Template Folder Structure (Recommended)
yourapp/
βββ templates/
β βββ students/
β βββ student_list.html
β βββ student_detail.html
β βββ student_form.html
β βββ student_confirm_delete.html
π§© Summary of URL Names
| View | Path | URL Name |
|---|---|---|
| List | /students/ | student-list |
| Detail | /students/<int:pk>/ | student-detail |
| Create | /students/create/ | student-create |
| Update | /students/<int:pk>/update/ | student-update |
| Delete | /students/<int:pk>/delete/ | student-delete |