Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

javascript - 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...