68
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).
18
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)
- [Django]-Django Setup Default Logging
- [Django]-Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet
- [Django]-Django – Getting last object created, simultaneous filters
5
from django.contrib import messages
class LectureDelete( DeleteView):
model = Lecture
def get_success_url(self):
messages.success(self.request, "deleted successfully")
return reverse("/")
- [Django]-OperationalError, no such column. Django
- [Django]-Can a dictionary be passed to django models on create?
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
3
If you want to overcome the exception problem as in Brett Thomas’s comment, you can update Tocino’s solution in this way:
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()
data_to_return = super(SessionDeleteView, self).delete(request, *args, **kwargs)
messages.success(self.request, self.success_message % obj.__dict__)
return data_to_return
- [Django]-With DEBUG=False, how can I log django exceptions to a log file
- [Django]-Disable migrations when running unit tests in Django 1.7
- [Django]-Django syncdb and an updated model
1
If using Django 4.0.0 or above the DeleteView
can be used together with SuccessMessageMixin
as per this Release Note
So in the following example which deletes the a User, the success message is displayed on the account logout page after the User is deleted.
class UserDeleteView(LoginRequiredMixin, SuccessMessageMixin, DeleteView):
success_message = _("Your account has been deleted")
success_url = reverse_lazy('account_logout')
def get_object(self):
return self.request.user
Something to watch out for when upgrading from older versions of Django to Django >= 4.0.0
As per the same Release Note: Custom delete logic in delete() handlers should be moved to form_valid(). We has success messages in delete
methods which stopped working after the Django 4.0.0 upgrade.
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
- [Django]-OneToOneField and Deleting
-1
It seems like you’re using the messages framework of django in your template but not in your view.
In your view, try adding your success message like this:
from django.contrib import messages
messages.success(request, "Die Veranstaltung wurde gelöscht")
- [Django]-How to encode UTF8 filename for HTTP headers? (Python, Django)
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
- [Django]-Django rest framework – filtering for serializer field