{"id":5885,"date":"2022-11-28T06:22:47","date_gmt":"2022-11-28T06:22:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5885"},"modified":"2023-08-17T09:27:09","modified_gmt":"2023-08-17T09:27:09","slug":"django-delete-form","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-delete-form\/","title":{"rendered":"Django Delete Form"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create a Django delete form to delete a post.<\/p>\n\n\n\n<p class=\"note\">This tutorial begins where&nbsp;creating&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-edit-form\/\">Django edit form<\/a> tutorial&nbsp;left off.<\/p>\n\n\n\n<p>We&#8217;ll create a form that deletes a post by its id.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-an-url-pattern'>Creating an URL pattern <a href=\"#creating-an-url-pattern\" class=\"anchor\" id=\"creating-an-url-pattern\" title=\"Anchor for Creating an URL pattern\">#<\/a><\/h2>\n\n\n\n<p>Add an URL pattern to the pattern list in the <code>urls.py<\/code> of the <code>blog<\/code> application:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> django.urls <span class=\"hljs-keyword\">import<\/span> path\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> . <span class=\"hljs-keyword\">import<\/span> views\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>urlpatterns = &#91;\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">''<\/span>, views.home, name=<span class=\"hljs-string\">'posts'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'post\/create'<\/span>, views.create_post, name=<span class=\"hljs-string\">'post-create'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'post\/edit\/&lt;int:id&gt;\/'<\/span>, views.edit_post, name=<span class=\"hljs-string\">'post-edit'<\/span>),\n<\/span><\/span><mark class='shcb-loc'><span>    path(<span class=\"hljs-string\">'post\/delete\/&lt;int:id&gt;\/'<\/span>, views.delete_post, name=<span class=\"hljs-string\">'post-delete'<\/span>),\n<\/span><\/mark><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'about\/'<\/span>, views.about, name=<span class=\"hljs-string\">'about'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>]\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The delete URL accepts an id as an integer that specifies the id of the post to be deleted. When you open the URL:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">http:\/\/<span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>\/post\/delete\/<span class=\"hljs-number\">1<\/span>\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Django will execute the <code>delete_post()<\/code> function in the <code>views.py<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-a-view-function'>Defining a view function <a href=\"#defining-a-view-function\" class=\"anchor\" id=\"defining-a-view-function\" title=\"Anchor for Defining a view function\">#<\/a><\/h2>\n\n\n\n<p>Define a <code>delete_post()<\/code> function in the <code>views.py<\/code> of the <code>blog<\/code> application:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> django.shortcuts <span class=\"hljs-keyword\">import<\/span> render,redirect, get_object_or_404\n<span class=\"hljs-keyword\">from<\/span> django.contrib <span class=\"hljs-keyword\">import<\/span> messages\n<span class=\"hljs-keyword\">from<\/span> .models <span class=\"hljs-keyword\">import<\/span> Post\n<span class=\"hljs-keyword\">from<\/span> .forms <span class=\"hljs-keyword\">import<\/span> PostForm\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">delete_post<\/span><span class=\"hljs-params\">(request, id)<\/span>:<\/span>\n    post = get_object_or_404(Post, pk=id)\n    context = {<span class=\"hljs-string\">'post'<\/span>: post}    \n    \n    <span class=\"hljs-keyword\">if<\/span> request.method == <span class=\"hljs-string\">'GET'<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> render(request, <span class=\"hljs-string\">'blog\/post_confirm_delete.html'<\/span>,context)\n    <span class=\"hljs-keyword\">elif<\/span> request.method == <span class=\"hljs-string\">'POST'<\/span>:\n        post.delete()\n        messages.success(request,  <span class=\"hljs-string\">'The post has been deleted successfully.'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> redirect(<span class=\"hljs-string\">'posts'<\/span>)\n\n<span class=\"hljs-comment\"># ...<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, get the post by id using the <code>get_object_or_404()<\/code> and render the <code>post_confirm_delete.html<\/code> template. If the post doesn&#8217;t exist, then redirects to a 404 page.<\/li>\n\n\n\n<li>Second, render the <code>post_confirm_delete.html<\/code> template if the HTTP request is GET.<\/li>\n\n\n\n<li>Third, delete the post, <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-flash-messages\/\">create a flash message<\/a>, and redirect to the post list if the HTTP request is POST.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-template'>Creating a template <a href=\"#creating-a-template\" class=\"anchor\" id=\"creating-a-template\" title=\"Anchor for Creating a template\">#<\/a><\/h2>\n\n\n\n<p>Create the <code>post_confirm_delete.html<\/code> template in the <code>templates\/blog<\/code> directory of the <code>blog<\/code> application. This template extends the <code>base.html<\/code> template of the project:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">{% extends 'base.html' %} \n\n{% block content %}\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>Delete Post<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">method<\/span>=<span class=\"hljs-string\">\"POST\"<\/span>&gt;<\/span>\n  {% csrf_token %}\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Are you sure that you want to delete the post \"{{post.title}}\"?<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span>&gt;<\/span>Yes, Delete<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'posts' %}\"<\/span>&gt;<\/span>Cancel<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span>\n\n{% endblock content %}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This template contains a form that has two buttons. If you click the submit button (<code>Yes, Delete<\/code>) it&#8217;ll send an HTTP POST request to the specified URL. Otherwise, it&#8217;ll navigate to the post list URL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-the-delete-link-to-the-post'>Adding the delete link to the post <a href=\"#adding-the-delete-link-to-the-post\" class=\"anchor\" id=\"adding-the-delete-link-to-the-post\" title=\"Anchor for Adding the delete link to the post\">#<\/a><\/h2>\n\n\n\n<p>Add the delete link to each post in the <code>home.html<\/code> template:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-code-table\"><span class='shcb-loc'><span>{% extends 'base.html' %}\n<\/span><\/span><span class='shcb-loc'><span>\t\n<\/span><\/span><span class='shcb-loc'><span>{% block content %}\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>My Posts<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t{% for post in posts %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>{{ post.title }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">small<\/span>&gt;<\/span>Published on {{ post.published_at | date:\"M d, Y\" }} by {{ post.author | title}}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">small<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>{{ post.content }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'post-edit' post.id %}\"<\/span>&gt;<\/span>Edit<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span> \n<\/span><\/span><mark class='shcb-loc'><span>\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'post-delete' post.id%}\"<\/span>&gt;<\/span>Delete<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t{% endfor %}\n<\/span><\/span><span class='shcb-loc'><span>{% endblock content %}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you open the URL <code>http:\/\/127.0.0.1\/<\/code>, you&#8217;ll see the delete link that appears next to the edit link:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"430\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-link.png\" alt=\"\" class=\"wp-image-5886\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-link.png 458w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-link-300x282.png 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/figure>\n\n\n\n<p>If you click the delete link, you&#8217;ll navigate to the delete URL. For example, the following shows the page when deleting the post with the title <code>\"Flat is better than nested***\"<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"169\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-confirmation.png\" alt=\"\" class=\"wp-image-5887\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-confirmation.png 472w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-confirmation-300x107.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/figure>\n\n\n\n<p>Once you click the <code>Yes, Delete<\/code> button, Django will execute the <code>delete_post()<\/code> function that deletes the post and redirects you to the post list:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"323\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-successfully.png\" alt=\"\" class=\"wp-image-5888\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-successfully.png 456w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-delete-form-delete-successfully-300x213.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<p class=\"note\"><a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_project_10.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Download the Django Project source code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>delete()<\/code> method to delete a model from the database.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5885\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-delete-form\/\"\n\t\t\t\tdata-post-title=\"Django Delete Form\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5885\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-delete-form\/\"\n\t\t\t\tdata-post-title=\"Django Delete Form\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to create a Django delete form to delete a post. This tutorial begins where&nbsp;creating&nbsp;Django edit form tutorial&nbsp;left off. We&#8217;ll create a form that deletes a post by its id. Creating an URL pattern # Add an URL pattern to the pattern list in the urls.py of the blog [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5885","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5885"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5885\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5531"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}