Flask-WTF是简化了WTForms操作的一个第三方库。WTForms表单的两个主要功能是验证用户提交数据的合法性以及渲染模板。当然还包括一些其他的功能:CSRF保护,文件上传等。安装Flask-WTF默认也会安装WTForms,因此使用以下命令来安装Flask-WTF
pip install flask-wtf
用于验证用户输入的数据的合法性
pip install email_validator
forms.py
from wtforms import Form, StringField, validators,IntegerField, ValidationError,BooleanField
from wtforms.validators import length, Regexp, EqualTo, Email, NumberRange, InputRequired, URL
class emailForm(Form):
email = StringField('邮箱:',validators=[Email()])
# pip install email_validator
age = IntegerField('年龄:',validators=[NumberRange(1,120)])
username = StringField('用户名:',validators=[length(min=3, max=10, message='请输入3-10位长度的用户名'),InputRequired(message='必须填写用户名')])
password = StringField('密码:', validators=[length(min=8, max=20, message='请输入8-20位长度的密码')])
password_repeat = StringField('确认密码:', validators=[ EqualTo('password', message="两次密码不一致")])
phone = StringField('联系电话:',validators=[Regexp(r'1[358]\d{9}',message='请输入有效的手机号')])
info = StringField('GitHub主页地址:',validators=[URL(message='请输入有效的Git地址')])
captcha = StringField('验证码:',validators=[length(min=4,max=4)])
def validate_captcha(self,field):
# 这里的名字是固定格式
print(type(field))
if field.data != '8jsk':
raise ValidationError('验证码错误')
app.py
from flask import Flask, request, render_template
from flask.views import MethodView
from forms import emailForm
app = Flask(__name__)
@app.route('/')
def index():
return "主页"
class DemoView(MethodView):
def get(self):
form = emailForm(request.form)
print("!!!")
return render_template('other.html', form = form )
def post(self):
form = emailForm(request.form)
if form.validate():
return "注册成功"
else:
print(form.errors)
for value in form.errors.values():
for l in value:
return l
# 这样可以输出 message里面的数据
app.add_url_rule('/email/', view_func=DemoView.as_view('DemoView'))
if __name__ == '__main__':
app.run()
templates/other.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="" method="post">
<table>
<tr>
<td>{
{ form.username.label }}td>
<td>{
{ form.username() }}td>
tr>
<tr>
<td>{
{form.password.label}}td>
<td>{
{ form.password()}}td>
tr>
<tr>
<td>{
{form.password_repeat.label}}td>
<td>{
{form.password_repeat()}}td>
tr>
<tr>
<td>{
{form.age.label}}td>
<td>{
{form.age()}}td>
tr>
<tr>
<td>{
{form.email.label}}td>
<td>{
{form.email()}}td>
tr>
<tr>
<td>{
{form.info.label}}td>
<td>{
{form.info()}}td>
tr>
<tr>
<td>{
{form.phone.label}}td>
<td>{
{form.phone()}}td>
tr>
<tr>
<td>{
{form.captcha.label}}td>
<td>{
{form.captcha()}}td>
tr>
table>
<input type="submit" value="提交">
form>
body>
html>
from flask_wtf import CSRFProtect
app = Flask(__name__)
CSRFProtect(app)
form表单_____________________________________
<input type="hidden" name="csrf_token" value="{
{csrf_token()}}">
flask_upload.py
from flask import Flask, request, render_template, send_from_directory, Response
import os
from werkzeug.utils import secure_filename
from forms import UploadForm
app = Flask(__name__)
@app.route("/")
def index():
return "首页"
@app.route("/upload/",methods=['GET','POST'])
def upload():
if request.method =='GET':
return render_template('upload.html')
else:
picture = request.files.get('picture')
filename = secure_filename(picture.filename)
print(filename)
picture.save(os.path.join('images', filename))
return "文件上传成功"
@app.route('/images//' )
def get_image(filename):
return send_from_directory('images',filename)
if __name__ == '__main__':
app.run()
forms.py
from wtforms import Form, FileField
from flask_wtf.file import FileAllowed, FileRequired
class UploadForm(Form):
# FileRequired 文件必须传
# FileAllowed 允许上传的类型
picture = FileField(validators=[FileRequired(), FileAllowed(['jpg', 'png', 'gif'])])
templates/upload.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="" method="post" enctype="multipart/form-data">
头像:<input type="file" name="picture">
<input type="submit" value="上传">
form>
body>
html>