Flask框架——消息闪现
目录
使用消息闪现
消息闪现分类
过滤闪现消息
上篇文章我们学习了Flask框架——flask-caching缓存,这篇文章我们来学习Flask框架——flash消息闪现。
良好的web应用程序中需要即使向用户提供反馈信息,例如:当用户输入信息点击提交后,网页会提示是否提交成功或提交信息有误等。
Flask框架通过闪现系统提供了一个简单的反馈方式。其基本工作原理为:在当前请求结束时记录一个消息,提供给当前请求或者下一个请求使用。例如:用户在A页面中操作出错后跳转到B页面,在B页面中展示了A页面的错误信息。这时就可以通过消息闪现将错误信息传递给B页面。
使用消息闪现
Flask框架提供了flash()方法来实现消息闪现,在视图函数使用flash()方法将消息传递到下一个请求,这个请求一般是模板。
flash()方法语法结构为:
flash(message,category)
其中:
message:必选,数据信息;
category:可选,闪现类型。
注意:使用消息闪现时,需要配置SECRET_KEY的值,该值可以是任意字符。
示例代码如下:
from flask import Flask, render_template, request, flashapp = Flask(__name__)
app.config['SECRET_KEY']='sfasf' #配置SECRET_KEY,该值为任意字符@app.route('/',methods=['GET','POST'])
def flash_message():if request.method=='POST':username=request.form.get('username') #获取提交的username值if username=='admin': #验证是否为adminflash('验证成功') #使用flash方法传递信息return render_template('index.html') #使用render_template()方法渲染模板index.htmlelse:flash('验证失败') #使用flash方法传递信息return render_template('index.html') #使用render_template()方法渲染模板index.htmlreturn render_template('login.html') #使用render_template()方法渲染模板login.htmlif __name__ == '__main__':app.run()
这里我们简单地通过获取用户填写信息进行判断是否正确来传递信息,在login.html模板代码为:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action="{{ url_for('flash_message') }}" method="post"><p><input type="text" name="username" placeholder="输入用户名"></p><p><input type="submit" value="提交"></p>
</form>
</body>
</html>
创建一个form表单并绑定上面视图函数中的flash_message(),创建文本框和按钮完成一个简单的提交网页。
在index.html模板中,使用get_flashed_messages()方法来获取消息,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
{% with messages=get_flashed_messages() %} {# 使用get_flashed_messages()方法获取消息#}{% if messages %} {% for message in messages %}{{ message }}{% endfor %}{% endif %}
{% endwith %}
</body>
</html>
注意:使用get_flashed_messages()方法获取的消息以元组的方式来保存,所以可以通过for循环来获取。
启动我们的flask程序,访问http://127.0.0.1:5000/,如下图所示:
Flask14---执行flask闪现
当我们输入admin并点击提交,就会跳转网页,该网页显示内容为:验证成功;
当我们输入其他字符点击提交,就会跳转网页,该网页显示内容为:验证失败。
消息闪现分类
使用flash()方法时,我们可以传递category参数,其值可以为error(错误)、info(信息)、warning(警告),示例代码如下所示:
from flask import Flask, render_template, request, flashapp = Flask(__name__)
app.config['SECRET_KEY']='sfasf' #配置SECRET_KEY,该值为任意字符@app.route('/',methods=['GET','POST'])
def flash_message():if request.method=='POST':username=request.form.get('username') #获取提交的username值if username=='admin': #验证是否为adminflash('验证成功','info') #使用flash方法传递信息,闪现类型为:inforeturn render_template('index.html') #使用render_template()方法渲染模板index.htmlelse:flash('验证失败','error') #使用flash方法传递信息,闪现类型为:errorreturn render_template('index.html') #使用render_template()方法渲染模板index.htmlreturn render_template('login.html') #使用render_template()方法渲染模板login.htmlif __name__ == '__main__':app.run()
在index.html模板中,在使用get_flashed_messages()时,需要传递with_categories参数,其值为true,表示接收闪现类型,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
{% with messages=get_flashed_messages(with_categories=true) %} {#使用get_flashed_messages()方法#}{% if messages %}<ul>{% for category , message in messages %}<li class="{{ category }}">{{ message }}</li>{% endfor %}</ul>{% endif %}
{% endwith %}
</body>
</html>
过滤闪现消息
当我们在视图函数中设置了多个闪现,这时我们可以在模板文件中使用get_flashed_messages()方法时传递category_filter参数来选择我们需要的展示的闪现消息,例如我们在上面的视图函数中flash('验证成功','info')代码下添加下面flash()闪现代码:
from flask import Flask, render_template, request, flashapp = Flask(__name__)
app.config['SECRET_KEY']='sfasf' #配置SECRET_KEY,该值为任意字符@app.route('/',methods=['GET','POST'])
def flash_message():if request.method=='POST':username=request.form.get('username') #获取提交的username值if username=='admin': #验证是否为adminflash('验证成功','info') #使用flash方法传递信息,闪现类型为:infoflash('请不要随意相信任何广告','warning') #使用flash方法传递信息,闪现类型为:warningreturn render_template('index.html') #使用render_template()方法渲染模板index.htmlelse:flash('验证失败','error') #使用flash方法传递信息,闪现类型为:errorreturn render_template('index.html') #使用render_template()方法渲染模板index.htmlreturn render_template('login.html') #使用render_template()方法渲染模板login.htmlif __name__ == '__main__':app.run()
在index.html模板文件中,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
{% with messages=get_flashed_messages(category_filter=['warning']) %} {#使用get_flashed_messages()方法#}{% if messages %}{% for message in messages %}{{ message }}{% endfor %}{% endif %}
{% endwith %}
</body>
</html>
这里我们过滤选择了warning类型的闪现类型,启动flask程序,访问http://127.0.0.1:5000/,
当我们输入admin并点击提交,就会跳转网页,该网页显示内容为:请不要随意相信任何广告;
注意:信息不能比Cookie大,否则会导致闪现静默失败。
好了,Flask框架——消息闪现就讲到这里了,感谢观看,下篇文章我们继续学习Flask框架——Flask-WTF表单:数据验证、CSRF保护。
公众号:白巧克力LIN
该公众号发布Python、数据库、Linux、Flask、自动化测试、Git等相关文章!
- END -
Flask框架——消息闪现相关推荐
- python web 框架的flash消息_python web开发-flask中消息闪现flash的应用
Flash中的消息闪现,在官方的解释是用来给用户做出反馈.不过实际上这个功能只是一个记录消息的方法,在某一个请求中记录消息,在下一个请求中获取消息,然后做相应的处理,也就是说flask只存在于两个相邻 ...
- flask web 框架——消息闪现
文章目录 一.消息闪现 二.闪现消息的类别 三.过滤闪现消息 一.消息闪现 Flask 通过闪现系统来提供了一个简单易用的反馈方式.闪现系统的基本工作原理是:在请求结束时记录一个消息,提供且只提供给下 ...
- flask框架之闪现消息提示
前言 这几年一直在it行业里摸爬滚打,一路走来,不少总结了一些python行业里的高频面试,看到大部分初入行的新鲜血液,还在为各样的面试题答案或收录有各种困难问题 于是乎,我自己开发了一款面试宝典,希 ...
- python flask flash消息闪现
flash 消息闪现 很多人都不用flash这个组件,其实特别好用. 好的应用和用户界面的重点是回馈.如果用户没有得到足够的反馈,他们可能最终会对您的应用产生不好的评价.Flask 提供了一个非常简单 ...
- python web开发-flask中消息闪现flash的应用
Flash中的消息闪现,在官方的解释是用来给用户做出反馈.不过实际上这个功能只是一个记录消息的方法,在某一个请求中记录消息,在下一个请求中获取消息,然后做相应的处理,也就是说flask只存在于两个相邻 ...
- Flask之消息闪现
原文地址 查看全文 http://www.taodudu.cc/news/show-3241814.html 相关文章: Flask之cookie.session.闪现 移动端银行卡识别技术带来了便捷 ...
- Flask框架——flask-caching缓存
目录 安装flask-caching 缓存类型 初始化 使用缓存 缓存视图函数 其他函数 缓存对象(键值对) 上篇文章我们学习了Flask框架--Session与Cookie,这篇文章我们来学习Fla ...
- Flask(十二)——消息闪现
一个基于GUI好的应用程序需要向用户提供交互的反馈信息. 例如,桌面应用程序使用对话框或消息框,JavaScript使用 alert() 函数用于类似的目的. 在Flask Web应用程序中生成这样的 ...
- Flask 重定向、错误和消息闪现
Flask 重定向和错误 Flask类有一个**redirect()**函数.调用时,它返回一个响应对象,并将用户重定向到具有指定状态代码的另一个目标位置. redirect函数 **redirect ...
最新文章
- mysql用户的创建和授权_MySQL用户创建和授权
- postgresql 新建decimal字段_postgresql路径规划插件pgrouting使用
- module 'scipy.misc' has no attribute 'imresize'
- github如何clone别人commit的历史版本的仓库
- ftp服务器压缩文件,ftp压缩服务器文件
- maven + spring mvc 创建Java web项目
- 首次运行 tensorflow 项目之 vgg 网络
- NHibernate 基础教程
- vue中引入高德地图获取坐标
- 如何做出3blue1brown的动画视频
- 用vbs写九九乘法表
- 关于数据库系统的查询处理
- 利用python进行识别相似图片(一)
- 腾讯即时通信 tim-sdk.js功能扩展
- 高品质摄影作图台式计算机推荐,能拍出高品质作品的强大系统 摄影师段岳衡专访...
- spring boot 三种类型事物实现说明
- 巴西柔术第三课:封闭式防守的降服技术
- HTML 网页相关概念
- 用Origin找两曲线的交点
- 计算机的医学应用,计算机技术在医学中的应用
热门文章
- 51单片机用定时器0实现流水灯
- 计算矢量图中的线长度和统计信息(QGIS)
- Android 开源网络框架OKHttp4 Kotlin版本源码解析
- Errors during downloading metadata for repository ‘AppStream 报错
- Sams Teach Yourself MySQL in 10 Minutes
- 在Ubuntu9.10上折腾Maemo SDK5的过程
- 理解RHEL上安装oracle的配置参数 :/etc/security/limits.conf, /etc/profile, /etc/pam.d/login
- Qt OpenGL(二十)——Qt OpenGL 核心模式版本
- AcWing 3465. 病毒朔源 (邻接表DFS 详解)
- Python Numpy鸢尾花实训,数据处理