[Answered ]-Django – Displaying alert messages in base template

2πŸ‘

Your base template should provide defaults for most or all pages, but make it possible to override the default in cases where it’s less than ideal.

Here, put the base template message code into a named block.

{% block default_messages %}
{% if messages %}
    {% for message in messages %}
    <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert" id="message">
      {{ message }}
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
       <span aria-hidden="true">&times;</span>
      </button>
    </div>
    {% endfor %}
{% endif %}
{% endblock default_messages %}

Now in any page where you find that the default is undesirable, you can override it. In page_template.html:

{% block default_messages %}
    <!-- delete the default, i.e. omit { { block.super } } -->
{% endblock default_messages %}

and handle messages explicitly, elsewhere in page_template.html, perhaps with a %include if the block of message code is going to be useful in multiple pages.

πŸ‘€nigel222

-1πŸ‘

{% block content %}
{% endblock %}

put these inside div tag

πŸ‘€Destinysam

Leave a comment