DeleteView中的success_message未显示

时间:2014-07-18 10:11:45

标签: django django-class-based-views

我有一个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

在我的success_url链接的模板中,我有以下代码,可以与其他消息一起使用:

{% 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 %}

但是没有显示该消息。我错过了什么吗?我在做什么?谢谢!

5 个答案:

答案 0 :(得分:32)

我认为Django问题跟踪器中的this issue应该回答你的问题。

  

SuccessMessageMixin挂钩form_valid DeleteView上不存在,以便将其消息发送给用户。

它还提供了另一种适合我的方式:

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(但我将其用于“创建”和“更新”视图)。 希望这将在Django的更高版本中得到改进(有关详细信息,请参阅issue)。

答案 1 :(得分:8)

The answer by Hel1大部分都是正确的,但是没有提供在成功消息中显示字段的解决方案,如下所示:

success_message = "Session %(name)s was removed successfully"

只需获取要删除的对象并使用对象的字典格式化字符串,如下所示:

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)

答案 2 :(得分:1)

如果您要克服Brett Thomas's comment中的异常问题,可以通过以下方式更新Tocino's solution

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

答案 3 :(得分:0)

from django.contrib import messages

class LectureDelete( DeleteView):
    model = Lecture

    def get_success_url(self):
        messages.success(self.request, "deleted successfully")
        return reverse("/")

答案 4 :(得分:-1)

您似乎在模板中使用了messages framework of django,但在视图中却没有。

在您看来,请尝试添加以下成功消息:

from django.contrib import messages

messages.success(request, "Die Veranstaltung wurde gelöscht")