I want to display flash messages in Django with the close button. Existing message framework in Django allows to display messages and does not allow to close it.
As an example, web2py provides such flash messages. I am looking for similar functionality in Django.
If it can be done with few lines of code , it would be great. I do not want to add any other libraries or framework on top of Django.
Thanks in advance.
I was unaware that such thing can be solved using boot-strap !
I did something like this :
{% if messages %}
{% for msg in messages %}
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{msg.message}}
</div>
{% endfor %}
{% endif %}
It shows message like :
in html template add this jquery timeout function
<script>
$(document).ready(function(){
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 5000);
});
</script>
Q. Dismiss Button is not working in alert message in django python
Ans: data-bs-dismiss="alert" ,this is the change in Bootstrap 5 i.e. in another bootstrap version there is data-dismiss="alert" , but in bootstrap 5 there is bs added so add bs like this data-bs-dismiss="alert"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With