Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase displaying time of django messages

In my app, whenever a question is deleted, it shows a django message that question is deleted. Related code is:

from django.contrib import messages

msg= _('Question is deleted')
messages.info(request, msg)

The message gets displayed as desired, however I want Display message to last longer say for minimum 10 sec. or till the user clicks on it.

In django docs saw expiration of messages but still could not figure out, and I have nothing like message storage which i can set to false.

Help appreciated :)

like image 679
The Recruit Avatar asked Dec 03 '25 11:12

The Recruit


1 Answers

The thing you want to do is javascript domain. Below code will display your messages for 10 sec or you can close it manually. In template you can do like this:

{% for message in messages %}
    <div class="message">
        {{ message }}
        <a href="#" class="del-msg">&times;</a>
    </div>
{% endfor %}

And in javascript:

<script>
    $(document).ready(function() {
        // messages timeout for 10 sec 
        setTimeout(function() {
            $('.message').fadeOut('slow');
        }, 10000); // <-- time in milliseconds, 1000 =  1 sec

        // delete message
        $('.del-msg').live('click',function(){
            $('.del-msg').parent().attr('style', 'display:none;');
        })
    });
</script>
like image 172
Zubair Afzal Avatar answered Dec 06 '25 03:12

Zubair Afzal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!