DeleteView و success_url
تأكيد عمليات الحذف ثم إعادة التوجيه
DeleteView و success_url درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Removing Records Safely
Sometimes data has to go. DeleteView handles removing a record, with a confirmation step built in. 🗑️
Meet DeleteView
DeleteView loads one object by its pk and prepares to delete it once the user confirms.
from django.views.generic import DeleteView
class PostDelete(DeleteView):
model = PostWhy Confirmation Matters
Deleting is permanent, so DeleteView shows a confirmation page on GET before anything is removed.
The Confirm Template
By default DeleteView looks for app/model_confirm_delete.html, like post_confirm_delete.html.
The Confirmation Form
The confirm page posts back to the same URL, and that POST request is what actually triggers the delete.
<form method="post">{% csrf_token %}
<p>Delete {{ object }}?</p>
<button>Yes, delete</button>
</form>Where success_url Comes In
After deleting, the user needs somewhere to go. success_url sets that destination explicitly.
from django.urls import reverse_lazy
class PostDelete(DeleteView):
model = Post
success_url = reverse_lazy("post_list")Why reverse_lazy Here
Use reverse_lazy, not reverse, because URLs are not ready when the class body is first read at import time.
reverse vs reverse_lazy
reverse resolves a URL immediately, while reverse_lazy waits until the value is actually needed.
Wiring the Delete URL
The delete URL captures a pk so DeleteView knows which record to remove.
path("<int:pk>/delete/", PostDelete.as_view()),Linking to Delete
From a detail page, link to delete using the url tag and the object pk so the right record is targeted.
<a href="{% url 'post_delete' object.pk %}">Delete</a>Safe by Design
The GET confirm plus POST delete pattern means no record vanishes from a stray click or a crawler. ✅
Quick Check
What does success_url control in a DeleteView?
Recap: Deleting Records
You used DeleteView with a GET confirm and POST delete, then sent users onward with success_url and reverse_lazy. 🎉
الأسئلة الشائعة
هل درس «DeleteView و success_url» مجاني؟
نعم — نص درس «DeleteView و success_url» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «DeleteView و success_url»؟
تأكيد عمليات الحذف ثم إعادة التوجيه تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «DeleteView و success_url»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ListView و DetailView
- CreateView و UpdateView
- DeleteView و success_url
- تجاوز get_queryset والقوالب