from flask.ext .wtf import Form from wtforms import StringField,SubmitField #字段类型类导入 字符串,提交from wtforms.validators import DataRequired #从表单验证 导入 请求
class NameForm(Form): name = StringField('你的名字 ?',validators=[DataRequired()]) #DataRequired()[验证函数]表示数据示为必须非空 submit = SubmitField('提交')创建一个NameForm类继承自Form, SrtingField返回表格里的 name信息@app.route('/',methods=['GET','POST']) #app后面的metheds参数告诉Flask 在URL映射中吧视图函数注册为Get Post请求的处理程序.如果没有metheds #参数,就只把视图函数注册为GET请求的处理程序 def user(): form = NameForm() if form.validate_on_submit():#如果数据能被所有验证函数接收(上面的)返回True 否则False old_name = session.get('name') if old_name is not None and old_name !=form.name.data: flash('看 你换了名字') session['name'] = form.name.data return redirect(url_for('user')) return render_template('user.html',form = form ,name=session.get('name'))session 应该是类似于cookies ,全部保存于客户端本地,且session是字典形式