Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide django message after it is displayed for few seconds

For example, before redirect, I used

messages.add_message(request, messages.INFO, 'You have successfully updated your listing.')

Then the message will be automatically displayed after redirection, however the message will never disappear, may I know how to hide it after few seconds?

Thanks!!!

like image 612
Zack Avatar asked Aug 04 '18 03:08

Zack


1 Answers

You need to write javascript code, use window setTimeout() method to hide message after some time.

// suppose the `id` attribute of element is `message_container`.
var message_ele = document.getElementById("message_container");

setTimeout(function(){ 
   message_ele.style.display = "none"; 
}, 3000);
// Timeout is 3 sec, you can change it

Put this code inside base.html at the bottom inside <script> tag, so whenever page reloaded after 3 sec it will make your message disappear.

like image 80
Satendra Avatar answered Sep 28 '22 18:09

Satendra