I have seen several posts that describe deleting items in Django with views and check boxes. I have accomplished this, and I have also accomplished deleting single items with AJAX. My current problem is that I cannot figure out how to delete multiple items with check boxes and AJAX in my Django application.
I'm using a generic view to delete:
urls.py
url(r'^image/delete/(?P<pk>\d+)/$', ImageDeleteView.as_view(), name=idv),
views.py
# when JS is not in play, the delete form kicks in and works with confirmation.
class ImageDeleteView(AjaxResponseMixin, DeleteView):
model = Image
form_class = DeleteImageForm
success_url = '/gallery/images/'
template_name = 'image_delete_form.html'
forms.py
class DeleteImageForm(forms.ModelForm):
class Meta:
model = GalleryImage
fields = []
ajax.js
var id = $(this).attr('id');
$.ajax({
type: 'post',
url: '/dashboard/image/delete/' + id + '/',
data: id,
success: function() {
console.log("AJAX call succeeded.");
location.reload();
},
error: function() {
console.log("AJAX call failed!");
location.reload();
}
});
For the template, I'm rendering a table with entries and listing image name, file path, etc, along with a check box and select all check box option (like Gmail). The single delete uses a button like that, when clicked, initiates the AJAX call.
image_list.html
<button type="button" class="delete" id="{{ image.pk }}">
[. . . OR . . .]
<input type="checkbox" id="{{ image.pk }}">
The single deletion works, but I cannot figure out what I need to do to delete multiple entries with this method.
Aucun commentaire:
Enregistrer un commentaire