问题描述
我正在与Ajax一起使用BootBox来删除应用程序中的列表,该列表称为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>