Search code examples
djangodjango-messages

Django messages: how to change html structure?


So I've got some really specific styling constraints for the messages, and I've pushed ::before and ::after as far as they will go. Beyond that what I really need is for the text of the message to be enclosed in a span tag, ideally with a settable class (it's just dumped in the div which is not great to begin with).

Is there any setting in Django, or any place I can go to restructure the html? I can't find any documentation for this (surely this is not an uncommon thing to customise).

The alternative is to use js, but I'd prefer to avoid cutting and pasting elements and content when it should be something that should be customisable.

What I've got is:

<div class="messages">
    <div class="alert alert-success alert-dismissable fade show">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">x</a>
        [message text]
    </div>
</div>

What I want is:

<div class="messages">
    <div class="alert alert-success alert-dismissable fade show">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">
            <i class="close-icon"></i>
        </a>
        <span>[message text]</span>
    </div>
</div>

In views.py I'm using SuccessMessageMixin in various places.


Solution

  • So as it was pointed out to me, this was my search fail. The html structure for the messages can usually be found in base.html (it seems like it is a standard place to set it up).

    There is some good documentation here: https://django-advanced-training.readthedocs.io/en/latest/features/contrib.messages/