python flask flash消息
- 2020 年 1 月 6 日
- 筆記
flash 消息閃現
很多人都不用flash這個組件,其實特別好用。 好的應用和用戶介面的重點是回饋。如果用戶沒有得到足夠的回饋,他們可能最終會對您的應用產生不好的評價。Flask 提供了一個非常簡單的方法來使用閃現系統向用戶回饋資訊。閃現系統使得在一個請求結束的時候記錄一個資訊,然後在且僅僅在下一個請求中訪問這個數據。這通常配合一個布局模板實現。
具體請查看: http://docs.jinkan.org/docs/flask/patterns/flashing.html
標準的bootstrap
template html
利用bootstrap的類的方法名來做這類的事情 success : 為綠色的 danger : 為紅色的 warning : 為×××的
/templates/flash.html
<body class="gray-bg"> <!--通知消息處--> {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} <div class="alert alert-{{ category }}" style="text-align: center"> <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button> <strong>{{ message }}</strong></div> {% endfor %} {% endif %} {% endwith %} </body>
controller action
下一個訪問請求之中獲取這個數據 /main.py
app.route("flash") def view_flash() flash(message, action) action = ["success", 'error', 'danger'] ... return render_template("flash.html")