DeleteView และ success_url
ยืนยันการลบและเปลี่ยนเส้นทางหลังจากนั้น
DeleteView และ success_url เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “DeleteView และ success_url”
ยืนยันการลบและเปลี่ยนเส้นทางหลังจากนั้น คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “DeleteView และ success_url” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ListView และ DetailView
- CreateView และ UpdateView
- DeleteView และ success_url
- การเขียนทับ get_queryset และเทมเพลต