Python Flask 闪光灯无法正常工作

Python Flask flash not working correctly

我有简单的两行代码:

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

我在 /contact 收到消息 'We are reachable at',但看起来是普通短信。它没有背景颜色(蓝色)或在几秒钟后消失。 其中 contact.html 包含

{% 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 %}

请看这个。这可能对你有帮助

<!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 %}

并使用 css

做一些造型
p {
 color:blue;  
 } 

并在代码中添加一些jquery

$(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);
})   

希望对您有所帮助。

我想你需要的是一些 CSS 让它看起来不错。如果你添加一些bootstrap到你的base.html/layout.html,你可以在base.html/layout.html.

中这样做
{% with messages=get_flashed_messages(with_categories=true) %}
 {% for category, message in messages %}
  <div class='alert alert-{{category}} text-center alert-dismissible fade show m-auto'>
    {{ message }}
   </div>
 {% endfor %}
{% endwith %}

现在,只要您要显示即显消息,就在您的路线中执行此操作。

@app.route('/')
def index():
   flash('Your message here', 'your bootstrap category[eg:success, primary, etc]')
   return reder_template('contact.html')