前言
一个好的应用和用户界面都需要良好的反馈。如果用户得不到足够的反馈,那么应用 最终会被用户唾弃。
Flask 的闪现系统提供了一个良好的反馈方式。闪现系统的基 本工作方式是:在且只在下一个请求中访问上一个请求结束时记录的消息。
设置 flash()
flask提供了一个非常有用的flash()函数,它可以用来“闪现”需要提示给用户的消息,比如当用户登录成功后显示“欢迎回来!”。
在视图函数调用flash()函数,传入消息内容,flash()函数把消息存储在session中,我们需要在模板中使用全局函数get_flashed_messages()获取消息并将它显示出来。
flash是基于session, 所以必须要设置秘钥 secret_key
flash()函数源代码
- message 消息内容
- category 消息类别,可以不用传,默认缺省值"message"
def flash(message: str, category: str = "message") -> None:
"""Flashes a message to the next request. In order to remove the
flashed message from the session and to display it to the user,
the template has to call :func:`get_flashed_messages`.
.. versionchanged:: 0.3
`category` p