简体   繁体   中英

Create pop up message in django

I am doing some calculations in my views.py and I want to show the result.

I have tried to use the following:

return render(request, index.html{message:variable}) 

and

return HttpResponse(message)

I want to show the result as a Popup message. How can I implement this?

first you have to render template by

return render_template( "example.html",message = any_variable)

and then you can show pop up on html via java script by

<script>

    alert("This is alert box!");  // display string message

    alert( {{message}} ); // display python variable

</script>

although i recommend using jinja as template

You can use Django messages https://docs.djangoproject.com/en/3.2/ref/contrib/messages/

from django.contrib import messages
messages.success(request, 'Success')

In your template, use something like:

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

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