I'm using Bootbox along with Ajax to delete a listing in my app which is calling a DeleteView but after I confirm the deletion, nothing changes.
View.py
class FileDelete(DeleteView):
model = Uploaded
success_url = reverse_lazy('index')
template_name = 'FileManagement/delete_file.html'
Script
<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")
I haven't finished the success part but it's still not deleting from the database.
You should use POST request instead of GET. Reference
Also, do not forget to include 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>