开发者学堂课程【Python Web 框架 Flask 快速入门:flash 消息闪现】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/603/detail/8791
flash 消息闪现
目录:
一、 flash 内容及目的
二、 Index
一、flash 内容及目的
from flash import Flask,render_template,request,flash
App=Flask(__name__)
App.secret_key‘itheima’
目的:实现一个简单的登录的逻辑处理
1. 路由需要有 get 和 post 两种请求方式-->需要判断请求方式
2. 获取请求的参数
3. 判断参数是否填写&密码是否相同
4. 如果判断都没有问题,就返回一个 success
给模板传递消息
flash--> 需要对内容加密,因此需要设置 secret_key ,做加密消息的混淆模板中需要遍历消息
@app.route(‘/’,methods=[‘GET’‘POST’])
det index():
request: 请求对象-->获取请求方式,数据
1.判断请求方式
if request.method==‘POST’
2.获取请求的参数
username=request.form.get(‘username’)
password=request.form.get(‘password’)
password2=request.form.get(‘password2’)
print password
3.判断参数是否填写&密码是否相同
if not all([username,password,password2])
print‘参数不完整’
flash (u‘参数不完整’)
elif password !=password2:
print ‘密码不一致’
flash (u‘密码不一致’)
else:
return ‘success’
return render_template(‘index.html’)
if __name__==‘__main__’:
app.run(debug=True)
二、Index
Title
用户名:
密码:
确认密码:
{#使用遍历获取闪现的消息#}
{% for message in get_flashed_messages()%}
{{ message }}
{% endfor %}