1.概述
- 通过wtform渲染模板
- 通过wtform验证表单数据
- 通过peewee保存数据
2.渲染模板
方法概述可以查看第35节内容
2.1后端
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from wtform36 import UserForm
class IndexHandle(RequestHandler):
async def get(self):
uf = UserForm()
self.render('personal36.html', uf = uf)
if __name__ == '__main__':
import os
# 获取绝对路径
base_path = os.path.abspath(os.path.dirname(__file__))
# 设置应用参数
settings = {
'template_path':os.path.join(base_path, 'templates'),
'static_path': os.path.join(base_path, 'static'),
'static_url_prefix': '/static/',
'debug': True
}
# 创建Tornado应用
app = Application([('/',IndexHandle)], **settings)
# 设置监听端口号
app.listen(8000)
IOLoop.current().start()
2.2wtforms的Form表单
from wtforms.fields.core import StringField
from wtforms.fields.simple import HiddenField
from wtforms_tornado import Form
class UserForm(Form):
id = HiddenField("ID")
username = StringField('用户名')
nick_name = StringField('昵称')
email = StringField('邮箱')
password = StringField('密码')
phone = StringField('电话')
language = StringField('语言')
2.3前端
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<!-- Title Page-->
<title>个人信息</title>
<!-- Bootstrap CSS-->
<link href="{{static_url('css/bootstrap.min.css')}}" rel="stylesheet" media="all">
<!-- Main CSS-->
<link href="{{static_url('css/theme.css')}}" rel="stylesheet" media="all">
</head>
<body class="animsition">
<div class="page-wrapper">
<div class="page-content--bge5">
<div class="container">
<div class="login-wrap">
<div class="login-content">
<div class="login-logo">
<a href="#">
<img src="{{static_url('img/logo2.png')}}" alt="CoolAdmin">
</a>
</div>
<div class="login-form">
<form action="/" method="post">
{% autoescape None %}
{% for f in uf %}
{% if f.label.text == 'ID' %}
<div class="form-group">
{{f(class_="au-input au-input--full", placeholder=f.label.text)}}
</div>
{% else %}
<div class="form-group">
{{f.label}}
{{f(class_="au-input au-input--full", placeholder=f.label.text)}}
</div>
{% end %}
{% end %}
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">确 认</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<!-- end document-->
3.数据的验证
3.1效果展示
3.2 后端
class IndexHandle(RequestHandler):
async def get(self):
uf = UserForm()
self.render('personal36.html', uf = uf)
async def post(self):
uf = UserForm(self.request.arguments)
# 验证数据
if uf.validate():
self.write('成功!!!')
else:
self.render('personal36.html', uf = uf)
3.3wtforms的表单
from wtforms.fields.core import StringField
from wtforms.fields.simple import HiddenField
from wtforms_tornado import Form
from wtforms.validators import DataRequired,Length
class UserForm(Form):
id = HiddenField("ID")
username = StringField('用户名',validators=(DataRequired(message='请输入用户名'),Length(min=3,max=10,message='请输入3-10位用户名')))
nick_name = StringField('昵称',validators=(DataRequired(message='请输入昵称'),))
email = StringField('邮箱',validators=(DataRequired(message='请输入邮箱'),))
password = StringField('密码',validators=(DataRequired(message='请输入密码'),Length(min=3,max=10,message='请输入3-10位用户名')))
phone = StringField('电话',validators=(DataRequired(message='请输入电话'),))
language = StringField('语言',validators=(DataRequired(message='请输入电话'),))
3.4前端
<form action="/" method="post">
{% autoescape None %}
{% for f in uf %}
{% if f.label.text == 'ID' %}
<div class="form-group">
{{f(class_="au-input au-input--full", placeholder=f.label.text)}}
</div>
{% else %}
<div class="form-group">
{{f.label}}
{{f(class_="au-input au-input--full", placeholder=f.label.text)}}
{% if f.errors %}
{{f.errors}}
{% end %}
</div>
{% end %}
{% end %}
4.peewee保存数据
4.1效果展示
4.2异步创建数据表
from peewee import *
import peewee_async
db = peewee_async.MySQLDatabase('tornado_db', host='127.0.0.1',port=3306,user='root',password='root')
manager = peewee_async.Manager(db)
class User(Model):
id = AutoField()
username = CharField(max_length=32)
nick_name = CharField(max_length=32)
email = CharField(max_length=32)
password = CharField(max_length=32)
phone = CharField(max_length=32)
language = CharField(max_length=32)
class Meta:
database = db
table_name = 't_user2'
if __name__ == '__main__':
User.create_table(True)
4.3后端
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from wtform36 import UserForm
from models38 import User,manager
class IndexHandle(RequestHandler):
async def get(self):
uf = UserForm()
self.render('personal36.html', uf = uf)
async def post(self):
uf = UserForm(self.request.arguments)
# 验证数据
if uf.validate():
del uf.data['id']
await manager.create(User, **uf.data)
# 数据的回显
self.render('personal36.html', uf = uf)
else:
self.render('personal36.html', uf = uf)
if __name__ == '__main__':
import os
# 获取绝对路径
base_path = os.path.abspath(os.path.dirname(__file__))
# 设置应用参数
settings = {
'template_path':os.path.join(base_path, 'templates'),
'static_path': os.path.join(base_path, 'static'),
'static_url_prefix': '/static/',
'debug': True
}
# 创建Tornado应用
app = Application([('/',IndexHandle)], **settings)
# 设置监听端口号
app.listen(8000)
IOLoop.current().start()