Search code examples
cssdjangodjango-messages

Django messages framework usage


About the messaging framework, in the docs it is written that every message has a message.tag property that can be uses for css. so my code looks like this

try:
    models.save()
    message.success(request, "Model successfully saved")
except DatabaseManagementTransaction:
    message.error(request, "Model could not be saved")

in my html template

{% if messages %}
    {% for message in messages %}
        <div class="alert alert-{{message.tag}} alert-dissmissable">
            {{message}}
        </div>
    {%endfor%}
{% endif %}

But when the template is rendered i see no message.tag and the div class looks like this

<div class="alert alert- alert-dissmissable">...</div>

So do i have to create MESSAGE_TAGS in settings file for this to work? Why is the message.tag empty? And another question. What happens to messages after they are presented to user? Are they deleted? If i add a new model will the previous messages be shown to me plus the newly appended one?


Solution

  • If should be tags as alert-{{message.tags}} in the template.

    What happens to messages after they are presented to user? Are they deleted?

    Yes, they are cleared once iterated (or displayed through template) from the storage. Refer message expiry.

    If i add a new model will the previous messages be shown to me plus the newly appended one?

    The messages list will have all currently active messages. So if previous message is still there it will be shown as well.