success_message in DeleteView not shown
The answer by Hel1 is mostly correct, but doesn't offer a solution for displaying fields within the success message, like this:
success_message = "Session %(name)s was removed successfully"
Simply get the object to be deleted and format the string with the object's dictionary, like this:
class SessionDeleteView(SuccessMessageMixin, DeleteView):
model = Session
success_url = reverse_lazy('session_home')
success_message = "Session %(name)s was removed successfully"
def delete(self, request, *args, **kwargs):
obj = self.get_object()
messages.success(self.request, self.success_message % obj.__dict__)
return super(SessionDeleteView, self).delete(request, *args, **kwargs)
I think this issue in the Django issue tracker should answer your question.
SuccessMessageMixin
hooks toform_valid
which is not present onDeleteView
to push its message to the user.
It also gives an alternative way which works for me:
from django.views.generic.edit import DeleteView
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages
from .models import Thing
class ThingDelete(DeleteView):
model = Thing
success_url = reverse_lazy('list')
success_message = "Thing was deleted successfully."
def delete(self, request, *args, **kwargs):
messages.success(self.request, self.success_message)
return super(ThingDelete, self).delete(request, *args, **kwargs)
SuccessMessageMixin
was not used in the delete-view (but I do use it for the Create and Update views).
Hopefully this will be improved in later versions of Django (see issue for more info).