DeleteView 不适用于 Django 中的 Ajax 和 Bootbox

DeleteView not working with Ajax and Bootbox in Django

我正在使用 Bootbox 和 Ajax 删除我的应用程序中调用 DeleteView 的列表,但在我确认删除后,没有任何变化。

View.py

class FileDelete(DeleteView):
    model = Uploaded
    success_url = reverse_lazy('index')
    template_name = 'FileManagement/delete_file.html'

脚本

<script>
    $(".delete-file").click(function () {
            var button = $(this);
            var id = button.attr("data-id");
            console.log(id);
            bootbox.confirm("Are you sure you want to delete this file?",
                function (result) {
                    if (result) {
                        $.ajax({
                            method: "GET",
                            url: "delete/" + id,
                            success: function(){

                         }
                      });
                    }
                });
        });
</script>

Urls.py

url(r'^delete/(?P<pk>\d+)/$', views.FileDelete.as_view(), name="delete_file")

我还没有完成成功部分,但它仍然没有从数据库中删除。

您应该使用 POST 请求而不是 GET。 Reference

此外,不要忘记包括 csrf_token

<script>
  $(".delete-file").click(function () {
    var button = $(this);
    var id = button.attr("data-id");
    console.log(id);
    bootbox.confirm("Are you sure you want to delete this file?", function (result) {
        if (result) {
          data = {
            csrfmiddlewaretoken: "{{ csrf_token }}",
            id: id
          }
          var posting = $.post("{% url 'delete_file' %}", data);
          posting.done(function (data) {
            // done
          });

          posting.fail(function (data) {
            // fail
          });
        }
    });
  });
</script>