简体   繁体   中英

Adding Pop-up Message in Django

i want to display popup message like (welcome <user>) when user will log in.
i can add this message to a section of the page by using Django messages

Like messages.success(request,welcome {request.user}')

and in template
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

but i don't want that. i just want a popup message for few seconds
How can i do that in a Django project?
Or, is there any way of doing that by using java script event listener?
Thanks is advance

When user will log in, you will have access to User object in and you can pass it to the view in the "context", which I am sure you are already doing. Then you can show an alert with the message or if you want something fancy you can use a Bootstrap modal.

https://getbootstrap.com/docs/4.0/components/modal/

{% if user %}
alert('Hello', '{{user.name}}'
{% endif %}

If you want to use Django messages:

{% if messages %}
alert('{{message}}')
{% endif %}
<li id="message_container" class="{{ message.tags }}">{{ message }}</li>

<script>
var message_ele = document.getElementById("message_container");

setTimeout(function(){ 
message_ele.style.display = "none"; 
}, 10000);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM