Skip to content
Advertisement

can’t close message in django

I’m new to Django and I tried to integrate message alerts in my code using a tutorial. These are showed fine but I can’t close them using the ‘x’ button.

enter image description here

This is the code for the message section:

{% for message in messages %}
<div class="container-fluid p-0">
  <div class="alert {{ message.tags }} alert-dismissible" role="alert" >
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
    {{ message }}
  </div>
</div>
{% endfor %}

Advertisement

Answer

Using this, messages automatically hide after 3 seconds and you can change it. Its better because no one just again and again close the message.

# Simple add id="message_box", here is the code:

{% for message in messages %}
<div class="container-fluid p-0">
  <div class="alert {{ message.tags }} alert-dismissible" role="alert" id="message_box">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
    {{ message }}
  </div>
</div>
{% endfor %}



Write this in your base.html.

<script>
var message_tag = document.getElementById("message_box");

setTimeout(function(){ 
   message_tag.style.display = "none"; 
}, 3000);    # 3000 is 3 seconds

</script>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement