DeleteViewとsuccess_url
削除を確認し、その後リダイレクトします
「DeleteViewとsuccess_url」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎉
AI チューターと学ぶ Python — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「DeleteViewとsuccess_url」レッスンは無料ですか?
はい。「DeleteViewとsuccess_url」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「DeleteViewとsuccess_url」で何を学びますか?
削除を確認し、その後リダイレクトします ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「DeleteViewとsuccess_url」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ListViewとDetailView
- CreateViewとUpdateView
- DeleteViewとsuccess_url
- get_querysetとTemplatesのオーバーライド