Displaying flash messages in DJango with close button
I want to display flash messages in Django using a close button. Django's existing message structure allows messages to be displayed and not to be closed.
As an example, web2py provides flash messages like this. I am looking for similar functionality in Django.
If it can be done with a few lines of code, that would be great. I don't want to add any other libraries or frameworks on top of Django.
Thanks in advance.
+3
doer_uvc
source
to share
2 answers
I didn't know that such a thing could be solved with a seat belt!
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 %}
A message like this is displayed here:
+3
doer_uvc
source
to share
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>
+4
Thameem
source
to share