环境:python2.7+flask+flask_wtf
内容:flask的部分常用表单标签
test.py
# -*- coding: utf-8 -*-
# flask web_form 表单控件
import flask
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.fields import (StringField, PasswordField, DateField, BooleanField,
SelectField, SelectMultipleField, TextAreaField,
RadioField, IntegerField, DecimalField, SubmitField)
from wtforms.validators import DataRequired, Length, Email, EqualTo, NumberRange
from flask_wtf.file import FileField, FileRequired, FileAllowed
from flask import Flask, render_template, redirect
app = Flask(__name__)
#from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
import os
from os import path
d = path.dirname(__file__)
###保护字段 app.config['SECRET_KEY'] = 'a random string'
'''
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)
'''
###定义表单
class NewsForm(FlaskForm):
# Text Field类型,文本输入框,必填,用户名长度为4到25之间
username = StringField('Username', validators=[Length(min=4, max=25)])
# Text Field类型,文本输入框,Email格式
email = StringField('Email Address', validators=[Email()])
# Text Field类型,密码输入框,必填,必须同confirm字段一致
password = PasswordField('Password', [
DataRequired(),
EqualTo('confirm', message='Passwords must match')
])
# Text Field类型,密码输入框
confirm = PasswordField('Repeat Password')
id = HiddenField('id', default='')
image = FileField(validators=[
FileRequired('no files'),
FileAllowed(['jpg', 'png'], 'Images only!')
])
# Text Field类型,文本输入框,必须输入整型数值,范围在16到70之间
age = IntegerField('Age', validators=[NumberRange(min=16, max=70)])
# Text Field类型,文本输入框,必须输入数值,显示时保留一位小数
height = DecimalField('Height (Centimeter)', places=1)
# Text Field类型,文本输入框,必须输入是"年-月-日"格式的日期
birthday = DateField('Birthday', format='%Y-%m-%d')
# Radio Box类型,单选框,choices里的内容会在ul标签里,里面每个项是(值,显示名)对
gender = RadioField('Gender', choices=[('m', 'Male'), ('f', 'Female')],validators=[DataRequired()])
# Select类型,下拉单选框,choices里的内容会在Option里,里面每个项是(值,显示名)对
job = SelectField('Job', choices=[
('teacher', 'Teacher'),
('doctor', 'Doctor'),
('engineer', 'Engineer'),
('lawyer', 'Lawyer')
]) # Select类型,多选框,choices里的内容会在Option里,里面每个项是(值,显示名)对
hobby = SelectMultipleField('Hobby', choices=[
('swim', 'Swimming'),
('skate', 'Skating'),
('hike', 'Hiking')
])
# Text Area类型,段落输入框
description = TextAreaField('Introduction of yourself')
# Checkbox类型,加上default='checked'即默认是选上的
accept_terms = BooleanField('I accept the Terms of Use', default='checked',
validators=[DataRequired()])
# Submit按钮
submit = SubmitField('Register')
###控制器
@app.route('/test/news_form', methods=['GET','POST'])
def NewsFormViews():
form = NewsForm()
if flask.request.method == "GET":
return render_template('news_form.html',form=form)
else:
#print form.image.data
if form.validate_on_submit():
if form.id.data:
print form.id.data
if form.image.data:
filename = form.image.data.filename
form.image.data.save(path.join(d,filename))
return redirect('/test/news_form')
else:
print form.errors
###把错误信息返回到页面
return render_template('news_form.html',form=form,error=form.errors) if __name__ == '__main__':
app.run(host='127.0.0.1',port='', debug=False)