Python Flask 闪存无法正常工作

标签 python flask jinja2

我有简单的两行代码:

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

希望对你有帮助。

关于Python Flask 闪存无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497236/

相关文章:

python - 诺尼尔 : Passing Python Function to Jinja2 Template

python - 如何在 Jinja2 模板中连接 int 和 str 类型?

python - 避免在 swarmplot 覆盖的 seaborn boxplot 中重复图例

python - Python 中的默认参数和 SQL 查询 - 习语/模式?

python - 在带有 python (django) 参数的游标/查询中使用 "like"

javascript - flask 只是刷新页面的模板化部分

python - 基于MySQL数据库使用Flask更新网页。

regex - Ansible:从 IP 地址中提取前三个八位字节

python - 从python 2.7中的列表插入数据库

Python 格式化 SQL WHERE 子句