Javascript 当您';你在删除帖子吗?

Javascript 当您';你在删除帖子吗?,javascript,python,django,django-views,django-templates,Javascript,Python,Django,Django Views,Django Templates,我在一个Django网站上工作,我会创建一个更简单的方法来深入研究一篇文章,比每次都进入一个新的HTML页面要好。有一种方法可以创建一个类似JavaScript警报(“文本”)的警报框,您可以单击“确定”来删除,或者单击“x”来维护帖子,还有一种方法可以不更改页面?代码是: url.py code... path('dashboard/delate/<slug:slug>', PostDeleteView.as_view(), name="delatepost")

我在一个Django网站上工作,我会创建一个更简单的方法来深入研究一篇文章,比每次都进入一个新的HTML页面要好。有一种方法可以创建一个类似JavaScript警报(“文本”)的警报框,您可以单击“确定”来删除,或者单击“x”来维护帖子,还有一种方法可以不更改页面?代码是:

url.py

code...
path('dashboard/delate/<slug:slug>', PostDeleteView.as_view(), name="delatepost"),
code...
class PostDeleteView(DeleteView):
    model = Post
    template_name = "admin/deletepost.html"
    success_url = reverse_lazy("listpost")

html

code...
{% extends "admin/layout.html" %}
{% block title %}
    <title>Add Post</title>
{% endblock title %} 
{% block content %}

<form enctype="multipart/form-data" method="POST">
<center>
    <div class="addpost">
        {% csrf_token %}
        <h1>delete {{ post.title }}?</h1>
        <button>Remove</button>
        </div>
    </center>
</form>

{% endblock content %}

代码。。。
{%extends“admin/layout.html”%}
{%block title%}
添加帖子
{%endblock title%}
{%block content%}
{%csrf_令牌%}
删除{{post.title}}?
去除
{%endblock内容%}

(现在没有身份验证,因为我正在我的计算机上测试)

您可以给按钮一个类并执行此操作

这里我假设你的按钮已经删除了帖子

document.querySelector("some container selector").addEventListener("click",function(e) {
  const tgt = e.target;
  if (tgt.classList.contains("delete")) {
    if (!confirm("Are you sure?")) e.preventDefault();
  }
})
如果没有,请使用AJAX:

document.querySelector("some container selector").addEventListener("click",function(e) {
  const tgt = e.target;
  if (!tgt.classList.contains("delete")) return 
  if (!confirm("Are you sure?")) return;

  const data = { postTitle: tgt.dataset.title };
  fetch('/delete', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', },
    body: JSON.stringify(data),
  })
  .then((response) => response.json())
  //Then with the data from the response in JSON...
  .then((data) => {
     console.log('Success:', data);
  })
  //Then with the error generated
  .catch((error) => {
     console.error('Error:', error);
  });
})
而且

<form id="myForm" enctype="multipart/form-data" method="POST">
<center>
    <div class="addpost">
        {% csrf_token %}
        <h1>delete {{ post.title }}?</h1>
        <button class="delete" data-title="{{ post.title }}">Remove</button>
        </div>
    </center>
</form>

{%csrf_令牌%}
删除{{post.title}}?
去除

您可以这样做,让JavaScript提交表单。这将向Django提交表单,Django也将执行验证/重定向

HTML

要做到这一点而不需要重新加载任何页面,请使用Django和查看AJAX请求

以下是一些资源:


这是一个无用的评论。您需要将
some container selector
更改为页面上的相关container selector,并将
class=“delete”
添加到按钮中。你这么做了吗?是的,我在寻找delate.html中没有的东西,一种逃离页面并在listpost.htmlw中执行remove方法的方法。我发布的方法更通用,它将事件处理程序附加到所有按钮的容器中,然后允许单击任何按钮来触发确认。我认为这是一个@Leonardo Obasso不理解我的建议的问题。你已经演示了如何创建确认对话框@mplungjan,但你从来没有演示过如何按照问题中的要求提交表单并将其删除。那么你的AJAX在哪里?我假设OP已经有了按钮工作。我意识到我也误解了这个问题,我的错。我添加了一些资源。@DeanElliott我也添加了
<form id="myForm" enctype="multipart/form-data" method="POST">
<center>
    <div class="addpost">
        {% csrf_token %}
        <h1>delete {{ post.title }}?</h1>
        <button id="removeButton">Remove</button>
        </div>
    </center>
</form>
let removeButton = document.getElementById('removeButton');
removeButton.onclick = function() {
    let accepted = confirm("Are you sure you want to delete this?");
    if (accepted) {
        document.getElementById('myForm').submit();
    }
}