When a user clicks a delete button, do not delete right away in the view that receives the GET request. Use Django’s DeleteView: GET only shows a confirmation page, POST actually deletes, and overriding get_queryset() lets users delete only their own records. This is both safer and far less code than a hand-written function view.

The Problem

Open a post list page, where each row has a “Delete” link pointing straight at the delete view. It sounds convenient, but a GET delete link is a trap: a crawling bot or a user clicking by accident can wipe out data with nobody asking twice. Worse, the view below does not check ownership — guessing an ID is enough to delete someone else’s post.

# views.py — ĐỪNG làm thế này
def post_delete(request, pk):
post = Post.objects.get(pk=pk)
post.delete()
return redirect("blog:post-list")

A single GET request to /post/42/delete/ makes the post disappear: no confirmation page, no login check, no author check. By HTTP standards, GET must be safe — it must not change data on the server.

The Solution

DeleteView does all three things correctly with just one short class:

# views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import DeleteView
from .models import Post
class PostDeleteView(LoginRequiredMixin, DeleteView):
model = Post
template_name = "blog/post_confirm_delete.html"
success_url = reverse_lazy("blog:post-list")
def get_queryset(self):
# Chỉ cho xóa bài của chính mình: bài người khác trả 404
return super().get_queryset().filter(author=self.request.user)
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path(
"post/<int:pk>/delete/",
views.PostDeleteView.as_view(),
name="post-delete",
),
]
<!-- blog/post_confirm_delete.html -->
<h1>Xóa bài viết "{{ object.title }}"?</h1>
<p>Bài viết sẽ bị xóa vĩnh viễn và không thể khôi phục.</p>
<form method="post">
{% csrf_token %}
<button type="submit">Xóa vĩnh viễn</button>
<a href="{% url 'blog:post-detail' object.pk %}">Hủy</a>
</form>

When a user opens the delete URL, DeleteView only renders the confirmation page — it does not touch the database. Only when they click “Delete permanently” does the POST form, with {% csrf_token %}, trigger the deletion, then redirect to success_url. Note: success_url is mandatory — unlike CreateView and UpdateView, DeleteView does not fall back to the model’s get_absolute_url(); without it the view raises ImproperlyConfigured.

The most important part is get_queryset(): filtering by author=request.user means other people’s posts never reach the view — Django returns a 404, without revealing that the post exists. LoginRequiredMixin, placed first, blocks guests who are not logged in.

If you need cleanup before deleting — for example removing an attached image file or writing a log — override form_valid(), do the extra work, then call super().form_valid(form) at the end.

Apply It to Your Project

Review every delete view in your project: any view that deletes data on a GET request must move to DeleteView, or at least require POST with {% csrf_token %}. For models with attached files (featured images, uploaded files), remember to clean up the files inside form_valid() before deleting the record — otherwise MEDIA_ROOT fills up with junk.

Related Posts

Frequently Asked Questions

Does DeleteView require a confirmation template?

Yes. GET needs to render a confirmation page, so a template is mandatory. If you do not set template_name, Django looks for the default file blog/post_confirm_delete.html. If neither exists, the view raises TemplateDoesNotExist.

What happens if I do not set success_url?

The view raises ImproperlyConfigured: “No URL to redirect to. Provide a success_url.” Unlike CreateView and UpdateView, DeleteView does not fall back to the model’s get_absolute_url(). For a dynamic redirect, override get_success_url() and return reverse_lazy(…) inside it.

Why not just write a function view with @require_POST?

You can, but you would have to rewrite everything DeleteView already provides: the confirmation page, a 404 when the record is not found, and the redirect after deletion. Using DeleteView means reusing tested code.

What about deleting records that have ForeignKey relations?

Deletion behavior depends on the on_delete of the related models: CASCADE deletes along with it, PROTECT blocks the deletion, SET_NULL only removes the link. Check the relations of a model before exposing a delete button, especially with CASCADE.

What if someone else guesses the delete URL?

No problem if you filtered in get_queryset() as above: posts that are not theirs return a 404. Combine it with LoginRequiredMixin to block guests who are not logged in from the start.

💬 Have you tried this in your own project? If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.

Sources