📝 Django

Django DeleteView

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
83
🌿
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

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 134
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 85
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

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