success_message in DeleteView not shown

北战南征 提交于 2019-12-20 09:53:30

问题


I have a DeleteView:

class LectureDelete(SuccessMessageMixin, DeleteView):
model = Lecture
success_message = "Die Veranstaltung wurde gelöscht"
success_url = '/'

def get_object(self):
    qs = super(LectureDelete, self).get_object()
    if self.request.user.has_perm('edit_lecture', qs):
        return qs
    else:
        raise exceptions.PermissionDenied

And in my template to which the success_url links, I have the following code, which works fine with other messages:

{% if messages %}
        {% for message in messages %}
        <p class="alert alert-dismissable {% if message.tags %}alert-{{ message.tags }}"{% endif %}>
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            {{ message }}
        </p>
        {% endfor %}
    {% endif %}

But the message is not shown. Am I missing something? What am I doing worng? Thanks!


回答1:


I think this issue in the Django issue tracker should answer your question.

SuccessMessageMixin hooks to form_valid which is not present on DeleteView 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).




回答2:


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)



回答3:


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")


来源:https://stackoverflow.com/questions/24822509/success-message-in-deleteview-not-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!