看不到任何SuccessMessage
。任何人都可以告诉我为什么成功创建数据库条目后为什么没有任何成功消息?
class TicketCreate(AdminPermissionRequiredMixin, SuccessMessageMixin, FormValidationMixin, BaseTicketView, TemplateView):
template_name = 'tickets/admin/create.html'
success_message = _("Ticket has been successfully created.")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['ticket_form'] = self.ticket_form
context['tax_form'] = self.tax_form
return context
@cached_property
def ticket_form(self):
return TicketForm(data=self.request.POST or None, event=self.request.event)
@cached_property
def tax_form(self):
return TicketTaxForm(prefix='tax', data=self.request.POST or None)
@transaction.atomic
def post(self, request, *args, **kwargs):
if self.ticket_form.is_valid() and self.tax_form.is_valid():
tax_instance = self.tax_form.save(commit=False)
tax_choice = self.tax_form.cleaned_data.get('tax_choice')
new_tax = (tax_choice == TicketTaxChoice.NEW_TAX and
tax_instance.name and tax_instance.percentage)
# save the tax instance
if new_tax:
tax_instance.event = self.request.event
tax_instance.save()
# save the ticket instance
ticket_instance = self.ticket_form.save(commit=False)
ticket_instance.event = self.request.event
if new_tax:
ticket_instance.tax = tax_instance
self.ticket_form.save()
return redirect(
'tickets:admin:detail',
self.request.organizer.slug,
self.request.event.slug,
ticket_instance.pk
)
return super().get(request, *args, **kwargs)
答案 0 :(得分:1)
将成功消息属性添加到基于
FormView
的类中
我看不到您的其他班级,但是从我看来,您的班级是基于TemplateView
的,它不是从FormView
派生的。
查看the source,您可以看到在调用form_valid
时显示了该消息,因此您的班级至少应指定form_class
或model
字段。但是您是手动调用is_valid()
,因此不会调用form_valid
,也不会显示消息。