gpt4 book ai didi

django - 未显示 DeleteView 中的成功消息

转载 作者:行者123 更新时间:2023-12-03 12:26:31 25 4
gpt4 key购买 nike

我有一个删除 View :

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

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

最佳答案

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

SuccessMessageMixin hooks to form_valid which is not present on DeleteView to push its message to the user.



它还提供了一种对我有用的替代方法:
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未在删除 View 中使用(但我确实将它用于创建和更新 View )。
希望这会在以后的 Django 版本中得到改进(更多信息见 issue)。

关于django - 未显示 DeleteView 中的成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24822509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com