Send mail after deleting django Model

Viewed 37

I've been using send_mail() for when someone creates a Group (model) or updates it, but I've been having trouble figuring out the function for deleting.

This works for when someone creates a Group:

class CreateGroup(CreateView):
    model = Group
    form_class = GroupForm
    template_name = 'create_group.html'

    def form_valid(self, form):
        instance = form.save()
        account = self.request.user.account
        send_mail (
            f"Thanks for creating {instance.name}",
            'create group',
            NOTIFICATION_EMAIL,
            [account.user.email],
            fail_silently=False
            )
        return HttpResponseRedirect(reverse('group_detail', args=[str(instance.pk)]))

class UpdateGroup(UpdateView):
    model = Group
    form_class = EditGroupForm
    template_name = 'update_group.html'

    def form_valid(self, form):
        instance = form.save()
        account = self.request.user.account
        send_mail (
                f"Thanks for updating {instance.name}",
                'update group',
                NOTIFICATION_EMAIL,
                [account.user.email],
                fail_silently=False
            )
        return HttpResponseRedirect(reverse('group_detail', args=[str(instance.pk)]))

That works, but how do I do it for DeleteView:

class DeleteGroup(DeleteView):
    model = Group
    template_name = 'delete_group.html'
    success_url = reverse_lazy('home')

Is there something like form_valid() for DeleteView? Or does form_valid() work for those views as well if there's a form associated. I assume a view needs a form for form valid and I thought there wasn't a form since I'm just deleting a Group.

1 Answers

Is there something like form_valid() for DeleteView?

As of , a DeleteView [Django-doc] uses the FormMixin [Django-doc], and thus has a .form_valid(…) method [Django-doc]. It works with a simple Form, that has no fields, and this is thus only used to simplify the logic, and will let Django validate the CSRF-token, and then delete the object.

Since , you thus can use .form_valid(…):

# since Django 4.0

class DeleteGroup(DeleteView):
    model = Group
    template_name = 'delete_group.html'
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        send_mail (
            f'Thanks for deleting{self.object}',
            'delete group',
            NOTIFICATION_EMAIL,
            [self.request.user.email],
            fail_silently=False
        )
        return super().form_valid(form)

Before , you can override the .delete(…) method [Django-doc], and send the email, so:

# beforeDjango 4.0

from django.http import HttpResponseRedirect


class DeleteGroup(DeleteView):
    model = Group
    template_name = 'delete_group.html'
    success_url = reverse_lazy('home')

    def delete(self, *args, **kwargs):
        self.object = self.get_object()
        success_url = self.get_success_url()
        send_mail (
            f'Thanks for deleting{self.object}',
            'delete group',
            NOTIFICATION_EMAIL,
            [self.request.user.email],
            fail_silently=False
        )
        return HttpResponseRedirect(success_url)

For deleting you send a POST or DELETE request to the view.

Related