我有一个CreateView和UpdateView,两者都有成功消息。但是,只有UpdateView成功消息有效,并且不显示CreateView消息。为什么会这样?
from django.contrib.messages.views import SuccessMessageMixin
class CreateRedirect (CreateView):
model = MarketingRedirect
template_name = 'marketing_redirect/create_redirect.html'
success_url = reverse_lazy('backend_redirect')
fields = ['redirect_from', 'redirect_to']
success_message = "Redirect successfully created!"
class EditRedirect(SuccessMessageMixin, UpdateView):
model = MarketingRedirect
fields = ['redirect_from', 'redirect_to']
template_name = 'marketing_redirect/edit_redirect.html'
context_object_name = 'redirect'
success_url = reverse_lazy('backend_redirect')
success_message = 'Review successfully updated'
答案 0 :(得分:5)
为类CreateRedirect继承 SuccessMessageMixin ,如下所示:
class CreateRedirect(SuccessMessageMixin, CreateView):
....