#demo1文件
#coding=utf-8
from flask import Flask,render_template ,request #render_template为模板
from config import Config
#导入flask_wtf提供的表单
from flask_wtf import FlaskForm
#导入表单类提供的字段
from wtforms import StringField,PasswordField,SubmitField
#导入表单类提供的验证函数
from wtforms.validators import DataRequired,EqualTo
app = Flask(__name__)
#使用配置文件
app.config.from_object(Config)
# 使用CSRF设密钥
app.config['SECRET_KEY'] = 'python24'
#需求:注册页面
#自定义表单类
class Form(FlaskForm):
# 定义表单字段/input/password/submit
user = StringField(validators=[DataRequired()])
pswd = PasswordField(validators=[DataRequired(),EqualTo('psed2')])
psw2 = PasswordField(validators=[DataRequired()])
submit = SubmitField(label=u'注册')
@app.route('/',method=['GET','POST'])
def demo2():
#实例化表单类对象
form = Form()
#form.validata_on_submit()函数会依次调用验证器,验证器条件满足,会查看表单页面中是否设置csrf_token
if form.validata_on_submit():
# 获取表单数据
us = form.user.data
ps = form.pswd.data
ps2 = form.pswd2.data
print us,ps,ps2
print form.validate_on_submit()
return render_template('login.html',form=form)
@app.route('/demo1',method=['GET','POST'])
def demo1():
#获取表单数据
user = request.form.get('user')
pswd = request.form.get('pswd')
return render_template('login.html')
if __name__=='__main__':
app.run()
"""demo1同级目录templates下的login.html"""
注册
登陆
{#手写input表单,登陆页面#}
"""demo1同级文件config.py"""
class Config:
DEBUG = True