๐Ÿ“ Django

Django DeleteView

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
300
๐ŸŒฟ
Level
Medium

DeleteView is a CBV for deleting an object with a confirmation step.

Basic example

from django.views.generic import DeleteView
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Task

class TaskDeleteView(LoginRequiredMixin, DeleteView):
    model = Task
    success_url = reverse_lazy('task-list')
    template_name = 'tasks/task_confirm_delete.html'

URL

path('tasks/<int:pk>/delete/', views.TaskDeleteView.as_view(), name='task-delete'),

Confirmation template

<h2>Delete task?</h2>
<p>Are you sure you want to delete "{{ object.title }}"?</p>

<form method="post">
  {% csrf_token %}
  <a href="{% url 'task-list' %}">Cancel</a>
  <button type="submit">Delete</button>
</form>

Permission check

class TaskDeleteView(LoginRequiredMixin, DeleteView):
    model = Task
    success_url = reverse_lazy('task-list')

    def get_queryset(self):
        # Users can only delete their own tasks
        return Task.objects.filter(owner=self.request.user)

Without a template (POST only)

class TaskDeleteView(LoginRequiredMixin, DeleteView):
    model = Task
    success_url = reverse_lazy('task-list')
    # Django looks for task_confirm_delete.html by default

Redirect after deletion

def get_success_url(self):
    return reverse('project-detail', kwargs={'pk': self.object.project.pk})

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 561
๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315

Did you like the article?

Subscribe to our updates and be the first to receive new articles. Grow with PyLand!