Joey Coder
Joey Coder

Reputation: 3489

Success message not displayed

Don't see any SuccessMessage. Anyone can tell me why I don't get any success message once I successfully created the database entry?

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)

Upvotes: 0

Views: 79

Answers (1)

UnholyRaven
UnholyRaven

Reputation: 407

From the official docs:

Adds a success message attribute to FormView based classes

I can't see your other classes, but from what I see your class is based on TemplateView, which is not derived from FormView. Looking the source you can see that message is shown when form_valid is called, so your class should at least specify form_class or model field. But you are calling is_valid() manually, so form_valid is not called and message is not shown.

Upvotes: 1

Related Questions