Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing flash messages in DJango with close button

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.

Flash Message in web2py

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.

like image 872
Utsav Chokshi Avatar asked Apr 22 '17 14:04

Utsav Chokshi


3 Answers

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">&times;</button>
      {{msg.message}}
    </div>
  {% endfor %}
{% endif %}

It shows message like : Flash Message in Django

like image 140
Utsav Chokshi Avatar answered Oct 09 '22 09:10

Utsav Chokshi


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>
like image 41
Thameem Avatar answered Oct 09 '22 08:10

Thameem


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"

like image 34
Sandy Avatar answered Oct 09 '22 09:10

Sandy