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.
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">×</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">×</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>