Change the default Django message tag

By default, messages.success outputs class='success'. Either I need to rewrite this, or delete it, or add tags to it to meet my needs. I could not find a way to rewrite and add to it. Here I tried to use extra_tags...

views.py

messages.success(request, '<a href="#">Item</a> Saved', extra_tags='html_safe alert alert-')

detail.html

I tried adding alertbefore {{ message.tags }}.

{% if messages %}
<ul class='messages'>
    {% for message in messages %}
    <li{% if message.tags %} class='{{ message.tags }}' role='alert'{% endif %}>{% if 'html_safe' in message.tags %}{{ message|safe }}{% else %}{{ message }}{% endif %}</li>
    {% endfor %}
</ul>
{% endif %}

Download Alerts Pending class='alert alert-success'

<div class="alert alert-success" role="alert">
    <strong>Well done!</strong> You successfully read this important alert message.
</div>

The result of the original HTML page

When all of the above code is executed, the source code outputs the following. The only problem now is the gap between alert-and success.

<ul class='messages'>
    <li class='html_safe alert alert- success' role='alert'><a href="#">Item</a> Saved</li>
</ul>

The end of the goal! - Can anyone see a hacker or the right workaround here?

<li class="html_safe alert alert-success" role="alert"><a href="#">Item</a> Saved</li>
+4
1

, . CSS - . :

messages.success(request, '<a href="#">Item</a> Saved', extra_tags='html_safe alert alert-success')

success. Django? kwargs, . :

<li class="html_safe alert alert-success success" role="alert"><a href="#">Item</a> Saved</li>

, .

, settings.py:

from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
    messages.SUCCESS: 'alert alert-success',
}

Django , success, , , , , . Bootstrap . , Django, Bootstrap. Bootstrap success, .

+4

All Articles