← Back to Django Course | Chapter 9: Class-Based Views | Lesson 7 of 10

DeleteView की मूल बातें

DeleteView एक ready-made confirmation page है जो database से एक row तब remove करता है जब आप confirm करते हैं कि आप वाकई delete करना चाहते हैं।
Syntax
markup
from django.views.generic.edit import DeleteView

class ModelNameDeleteView(DeleteView):
    model = ModelName
    success_url = '/success-url/'

# template: a <form method="post"> with {% csrf_token %} and a confirm button

एक Minimal DeleteView

DeleteView को model और success_url चाहिए; इसके URL को GET से visit करना एक confirmation page दिखाता है, और उस page के form को POST से submit करना असल में object delete करता है।

उदाहरण: A Minimal DeleteView

GET requests to BookDeleteView show a confirmation template; POST requests actually delete the Book.

markup
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
from .models import Book


class BookDeleteView(DeleteView):
    model = Book
    success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Confirmation Template

Confirmation template में एक form होना चाहिए जो उसी URL पर POST back करे, CSRF token सहित, क्योंकि GET से कभी कुछ delete नहीं होता।

Warning: बिना किसी confirmation form field वाला एक DeleteView users को deletion actually trigger करने का कोई तरीका नहीं देगा।

उदाहरण: The Confirmation Template

The object variable is the Book about to be deleted; the form's POST is what actually triggers the deletion.

markup
<p>Are you sure you want to delete "{{ object.title }}"?</p>
<form method="post">
  {% csrf_token %}
  <button type="submit">Delete</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Deletion के बाद Redirect करना

success_url DeleteView को बताता है कि object database से हटाए जाने के बाद browser को कहां भेजना है, typically एक list page पर वापस।

उदाहरण: Redirecting After Deletion

reverse_lazy("book-list") is resolved after deletion completes, sending the user back to the book list page.

markup
from django.urls import reverse_lazy
from django.views.generic.edit import DeleteView
from .models import Book


class BookDeleteView(DeleteView):
    model = Book
    success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक confirmation template जोड़ना भूल जाना, और यह surprise होना कि एक simple GET request पर delete नहीं होता।
  2. success_url set न करना, जिससे successful deletion के बाद Django error raise करता है क्योंकि इसे नहीं पता कि कहां redirect करना है।
  3. Delete link को plain GET link के रूप में wire करना बजाय POST form के, जो intended confirm-then-POST flow को तोड़ता है।
चैप्टर सारांश
  • DeleteView GET पर एक confirmation page दिखाता है और object को सिर्फ POST पर delete करता है।
  • template_name default रूप से <app>/<model>_confirm_delete.html होता है।
  • success_url set होना चाहिए (या get_success_url override किया जाना चाहिए) ताकि Django को पता हो कि deletion के बाद कहां redirect करना है।
  • Confirmation template को असली delete trigger करने के लिए एक CSRF token के साथ एक <form method="post"> चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.