Python Flask闪存无法正常工作

时间:2015-05-28 04:24:11

标签: python flask jinja2

我简单的2行代码:

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

我收到了消息'我们可以通过'在/联系,但它似乎是正常的短信。它没有背景颜色(蓝色)或在几秒钟后消失。 其中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 %}

2 个答案:

答案 0 :(得分:6)

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

<!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);
})   

希望这会对你有所帮助。

答案 1 :(得分:1)

我认为您需要的是一些 CSS 以使其看起来不错。如果您在 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')