How to add a Delete button to django.forms generated edit form (note, NOT admin)?
Its very easy to add a delete view on (/app/model/<id>/delete/
etc) but how to add a "Delete" button alongside the generated form?
I've got to be missing something easy?
Add a submit button to the template, set the name as 'delete', check in your view if it was clicked:
if request.POST.get('delete'):
obj.delete()
You could use some generic form like this
class DeletableModelForm(forms.ModelForm):
"""
Model form that allows you to delete the object
"""
delete = forms.BooleanField(
initial=False,
help_text=_('Check this to delete this object')
)
def save(self, commit=True):
if self.cleaned_data['delete']:
return self.instance.delete()
return super(DeletableModelForm, self).save()
And then you could restyle the checkbox to look like button. But you are probably better of with normal button with name...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With