①导入FlaskForm扩展包(from flask_wtf import FlaskForm)
②导入StringField,PasswordField,SubmitField (from wtforms import StringField,PasswordField,SubmitField)
③定义表单类
class RegisterForm(FlaskForm):
username = StringField('账号:')
password = PasswordField('密码:')
password2 = PasswordField('确认密码:')
submit=SubmitField('提交')
④定义路由
@app.route('/regist',methods=['GET','POST'])
def regist_form():
redist_form=RegisterForm()
return render_template('registform.html',form=redist_form)
⑤html文件中实现表单
完整代码:
app.py
from flask import Flask,render_template,request,flash
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField,SubmitField
app = Flask(__name__)
app.secret_key='yingjie'
class RegisterForm(FlaskForm):
username = StringField('账号:')
password = PasswordField('密码:')
password2 = PasswordField('确认密码:')
submit=SubmitField('提交')
@app.route('/regist',methods=['GET','POST'])
def regist_form():
redist_form=RegisterForm()
return render_template('registform.html',form=redist_form)
if __name__ == '__main__':
app.run()
registform.html
运行结果:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章