Python Flask flash not working correctly

我的梦境 提交于 2020-01-03 06:39:07

问题


I've simple 2 line code :

@app.route('/contact/')
def contact():
  flash('We are reachable at ')
  return render_template('contact.html')

I get the message 'We are reachable at' at /contact but it appears are normal text message. It doesn't background color(blue) or disappears after seconds. where contact.html contains

{% extends "layout.html" %}

{% block title %}Contact{% endblock title %}

{% block body %}
  <h2> Contact Us  </h2>
   Your email address must be valid as this is where reply
   will be sent. We do not share this address with anybody.

{% endblock body %}

回答1:


Please have a look at this. this might help you

<!doctype html>
<title>My Application</title>
 {% with messages = get_flashed_messages() %}
   {% if messages %}
     <ul class="flashes">
        {% for message in messages %}
         <div class="message_flash">{{ message }}</div>
        {% endfor %}
    </ul>
  {% endif %}
 {% endwith %}
{% block body %}
{% endblock %}

and Do some styling with the css

p {
 color:blue;  
 } 

And add some jquery to the code

$(function() {
// setTimeout() function will be fired after page is loaded
// it will wait for 5 sec. and then will fire
// $(".message_flash").hide() function
  setTimeout(function() {
      $(".message_flash").hide('blind', {}, 500)
  }, 5000);
})   

Hope this helps you.



来源:https://stackoverflow.com/questions/30497236/python-flask-flash-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!