服务端代码:
#学会了写表单类,如何将表单类渲染成HTML呢,此处可以结合前面学过的模板标签来进行渲染。#一、服务端代码#1、test_corms.py 文件#coding:utf-8from flask import Flask, render_templatefrom forms import LoginFormapp = Flask(__name__)@app.route('/')def login(): '''访问首页''' form = LoginForm() return render_template("from1.html",form=form)#预防重复提交app.config['SECRET_KEY'] = 'ddu384783274*%%Hgdiwre@#$%^&*de'if __name__ == '__main__': app.run(debug=True)#2、forms.py文件#coding:utf-8from flask_wtf import FlaskFormfrom wtforms import StringField, SubmitField, SelectField, RadioField,TextAreaField, \ DateField, BooleanFieldfrom wtforms.validators import DataRequiredfrom wtforms.widgets import CheckboxInput, PasswordInputclass LoginForm(FlaskForm): '''定义登录表单''' username = StringField('用户名',validators=[DataRequired()], render_kw={"required":"required","class":"form-controal"}) password = StringField('密码', validators=[DataRequired()], widget=PasswordInput()) language = SelectField('编程语言', choices = [('cpp','c++'),('py','python'),('java','Java')]) code = TextAreaField('代码') date = DateField('日期') is_checked = BooleanField('是否已经通过验证') submit = SubmitField('登录')
前端代码:
flask-wtf 表单渲染