文件位置关系:
app.py文件
# app.py
# made by 杨庆华
from flask_wtf import FlaskForm
from flask import Flask,render_template,request,flash
from wtforms import StringField, PasswordField, BooleanField,SubmitField
from wtforms.validators import DataRequired
'''
1路由的get或者post
2获取请求参数
3判断参数是否与密码相同
4如果判断没问题,返回一个success
'''
app = Flask(__name__)
# 用于做加密秘钥混淆的
app.secret_key = 'secret string'
@app.route('/',methods=['GET','POST'])
def index():
#request 是请求对象
if request.method == 'POST':
#通过request获取表单数据
username = request.form.get('username')
password = request.form.get('password')
password2 = request.form.get('password2')
#判断是否与密码相同
if not all([username,password,password2]):
flash(u'参数不完整')
elif password != password2:
flash(u"密码不一致")
else:
return 'success'
return render_template('index.html')
if __name__=='__main__':
app.run(debug=True)
index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" method="post">
<label>用户名</label><input type="text" name="username"><br>
<label>密码:</label><input type="password" name="password"><br>
<label>确认密码:</label><input type="password" name="password2"><br>
<input type="submit" value="提交">
{% for message in get_flashed_messages() %}
{# 注意messages()是方法有括号#}
{{message}}
{% endfor %}
</form>
</body>
</html>
可以发现提示信息可以在html上面显示 不需要再看控制台了