1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| from flask import Flask from flask import render_template from flask_wtf import FlaskForm from wtforms.fields import (StringField, PasswordField, DateField, BooleanField, SelectField, SelectMultipleField, TextAreaField, RadioField, IntegerField, DecimalField, SubmitField) from wtforms.validators import DataRequired, InputRequired, Length, Email, EqualTo, NumberRange
app = Flask(__name__) app.secret_key = 'asdfs'
class WtfForm(FlaskForm): username = StringField('用户名:', validators=[Length(min=4, max=25)], render_kw={'placeholder': '请输入用户名'})
email = StringField('邮箱地址:', validators=[Email()], render_kw={'placeholder': '请输入邮箱地址'})
password = PasswordField('密码:', validators=[DataRequired()], render_kw={'placeholder': '请输入密码'})
password2 = PasswordField('确认密码:', validators=[InputRequired(), EqualTo('password', '两次密码要一致')])
age = IntegerField('年龄:', validators=[NumberRange(min=16, max=70)])
height = DecimalField('身高(cm):', places=1)
birthday = DateField('出生日期:', format='%Y-%m-%d')
gender = RadioField('性别:', choices=[('0', '男'), ('1', '女')], validators=[DataRequired()])
job = SelectField('职业:', choices=[ ('teacher', '教师'), ('doctor', '医生'), ('engineer', '工程师'), ('lawyer', '律师') ])
hobby = SelectMultipleField('爱好:', choices=[ ('0', '吃饭'), ('1', '睡觉'), ('2', '敲代码') ])
description = TextAreaField('自我介绍:', validators=[InputRequired()], render_kw={'placeholder': '例:小明,18岁,未婚找女友'})
accept_terms = BooleanField('是否接受上述条款', default='checked', validators=[DataRequired()])
submit = SubmitField('提交')
@app.route('/', methods=['POST', 'GET']) def index(): form = WtfForm() return render_template('wtf.html', form=form)
if __name__ == "__main__": app.run(debug=True)
|