DeleteView की मूल बातें
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.
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 नहीं होता।
उदाहरण: The Confirmation Template
The object variable is the Book about to be deleted; the form's POST is what actually triggers the deletion.
<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.
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. #}
- एक confirmation template जोड़ना भूल जाना, और यह surprise होना कि एक simple GET request पर delete नहीं होता।
success_urlset न करना, जिससे successful deletion के बाद Django error raise करता है क्योंकि इसे नहीं पता कि कहां redirect करना है।- 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"> चाहिए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: