0

I'm trying to pass a dictionary through the "get_success_message" function but I just can't. I need more than a String on my message (title, color...) What am I doing wrong?

My view:

class PeliculasEdit(SuccessMessageMixin, UpdateView):
    model = Pelicula
    form_class = PeliculaForm
    template_name = "videoclub/peliculas_edit.html"
    success_url = reverse_lazy('peliculas_manage')
    message_data = {
        'color':'success',
        'titulo':'Película editada',
        'mensaje':'La película se ha editado correctamente',
    }

    def get_success_message(self, cleaned_data):
        return self.message_data

My template where i'm using the message:

{% if messages %}

    {% for message in messages %}
    <div class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
        </div>
        <div class="toast-body">
            <p>{{message.color}}</p>
        </div>
      </div>
    {% endfor %}

{% endif %}

I thought i had to {{message.color}} for example. But that's wrong. Thank you in advance!

coler-j
  • 1,791
  • 1
  • 27
  • 57
AngelQuesada
  • 387
  • 4
  • 19

1 Answers1

3

You cannot pass a dictionary in this way using the SuccessMessageMixin and the get_success_message method.

The Django docs say this about the get_success_message function:

The cleaned data from the form is available for string interpolation using the %(field_name)s syntax. For ModelForms, if you need access to fields from the saved object override the get_success_message() method.

The returned value of the get_success_message function is the message attribute which is a string sent to the messages framework. The SuccessMessageMixin is a shortcut for creating a success message, such as this:

messages.add_message(request, messages.SUCCESS, 'Profile details updated.')

It sounds like what you are looking for is how to add extra tags to a message:

messages.add_message(request, messages.SUCCESS, 'My success message string', extra_tags='color_success')

This is a duplicate of How can I add additional data to Django messages?

coler-j
  • 1,791
  • 1
  • 27
  • 57