1

I'd like to flash a message in a template, which there is a ton of information on, but I haven't seen any way to give it a custom name, which would allow it to have the appropriate class or id for styling. In Laravel:

webpage.php

@if (session()->has('login_success'))
    <div id="loginFlash">{{ session('login_success') }}</div>
@endif

Where login_success is defined before rendering the page

So far with Django:

webpage.html

{% for message in messages %}
    <div id="{{ message.tags }}">
        {{ message }}
    </div>
{% endfor %}

But I can't get message.tags to return a SINGLE custom value. For instance:

messages.add_message(self.request, messages.INFO, self.request.user.username, 'loginFlash')

This returns:

<div id="loginFlash info">
    username
</div>

How to remove the second argument? It seems like all of the message methods already specify at least one tag. Thanks so much!

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Tunn
  • 1,506
  • 16
  • 25

2 Answers2

0

You can customise the tags.messages output to customise your own tags/classes per message category by overriding the setting for that level, like this:

https://docs.djangoproject.com/en/2.0/ref/contrib/messages/#message-tags

https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-MESSAGE_TAGS

jhrr
  • 1,624
  • 14
  • 22
0

Needs another if statement, but gets the job done:

{% for message in messages %}
    {% if 'loginFlash' in message.tags %}
        <div id="loginFlash">{{ message }}</div>
    {% endif %}
{% endfor %}
Tunn
  • 1,506
  • 16
  • 25