当前位置: 首页 > news >正文

微网站可以做成域名访问媒体吧软文平台

微网站可以做成域名访问,媒体吧软文平台,互联网推广营销隐迅推认定,网站建设装修1.Tornado_wtform介绍 WTForms是用于Python Web开发的灵活的表单验证喝呈现库。他可以和任何的web框架和模板引擎一起使用。 现在因为前后端分离的原因,一般只用于表单的验证,模板渲染的功能基本不再使用。 2.为什么要表单数据验证 其实前端也是可以进…

1.Tornado_wtform介绍

WTForms是用于Python Web开发的灵活的表单验证喝呈现库。他可以和任何的web框架和模板引擎一起使用。
现在因为前后端分离的原因,一般只用于表单的验证,模板渲染的功能基本不再使用。

2.为什么要表单数据验证

其实前端也是可以进行数据验证,但由于前后端分离的原因,还是在后端进一步验证可以保证传入到的数据合法。

3.安装

pip install wtforms-tornado

4.使用方式

  1. 创建表单类
    1. 需要继承wtforms_tornado.Form
    2. 定义字段类型
    - 可以使用wtforms.fields引入
    3. 定义验证规则
    - 可以使用wtforms.validators引入
  2. 创建表单对象
  3. 调用表单验证方法

5.Tornado_wtform数据验证

这里将使用原先的写过的个人信息填写,对其进行添加数据验证功能
个人信息案例原网址

5.1错误信息解决:

ImportError: cannot import name ‘compat’ from ‘wtforms’

  1. wtforms===3.0.1 删除 pip uninstall wtforms
  2. 下载旧版本pip install wtforms==2.3.3

5.2代码展示

from wtforms34 import UserForm# 建立表单对象uf = UserForm(self.request.arguments)# 验证if not uf.validate():# 成功:true 失败:falseimport jsondata = json.dumps(uf.errors, ensure_ascii=False)self.write(data)

表单验证规则:

from wtforms_tornado import Form
from wtforms.fields import IntegerField, StringField
from wtforms.validators import DataRequired, Length# 创建一个表单类
class UserForm(Form):# 定义字段:注意 验证字段的名字一定要和前端传递来的名字保持一致# 编写规则id = IntegerField('ID')username = StringField('用户名',validators=(DataRequired(message='请填写用户名'),Length(min=3,max=10,message='请输入3-10位的用户名')))nick_name = StringField('昵称')email = StringField('邮箱')password = StringField('密码')phone = StringField('电话')language = StringField('语言')

6.完整代码

from tornado.web import Application, RequestHandler, URLSpec
from tornado.ioloop import IOLoop
import asyncio
import aiomysql
# 引入
from wtforms34 import UserFormclass IndexHandle(RequestHandler):# 首先由Application创建路由地址时,携带参数传递会到initialize,去定义一个self.mysql用于get中获取参数def initialize(self,mysql):self.mysql = mysqlasync def get(self):print(self.mysql)# 获取1个客户端链接池async with aiomysql.create_pool(host=self.mysql.get('host'),port=self.mysql.get('port'),user=self.mysql.get('user'),password=self.mysql.get('pwd'),db=self.mysql.get('db')) as pool:# 获取1个链接,用来获取游标async with pool.acquire() as con:# 获取一个游标,用来操作数据库async with con.cursor() as cur:# 执行sql# sql = 'select 101'sql = 'select * from t_user'await cur.execute(sql)# 获取结果rs = await cur.fetchone()print(rs)self.render('personal34.html',user = rs)async def post(self):# 获取前端传递来的数据uname = self.get_argument('username')nick_name = self.get_body_argument('nick_name')email = self.get_argument('email')password = self.get_body_argument('password')phone = self.get_argument('phone')language = self.get_body_argument('language')# 建立表单对象uf = UserForm(self.request.arguments)# 验证if not uf.validate():# 成功:true 失败:falseimport jsondata = json.dumps(uf.errors, ensure_ascii=False)self.write(data)else:try:# 获取idid = self.get_body_argument('id')except Exception as e:id = Falseargs = [uname,nick_name,email,password,phone,language]# 链接数据库# 获取1个客户端链接池async with aiomysql.create_pool(host=self.mysql.get('host'),port=self.mysql.get('port'),user=self.mysql.get('user'),password=self.mysql.get('pwd'),db=self.mysql.get('db')) as pool:# 获取1个链接,用来获取游标async with pool.acquire() as con:# 获取一个游标,用来操作数据库async with con.cursor() as cur:if not id:sql = 'insert into t_user values(0, %s, %s, %s, %s, %s, %s)'await cur.execute(sql, args) # 提交事务await con.commit()# 获取生成的idid = cur.lastrowidelse:sql = 'update t_user set uname=%s, nick_name=%s, email=%s, pwd=%s, phone=%s, language=%s where id=%s'# 增加id来告诉数据库更新哪一条数据args.append(id)await cur.execute(sql, args)await con.commit()# 存放id到args中args.insert(0,id)self.render('personal34.html', user = args)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,# 为了方便数据库的修改,可以直接把参数单独放在这'mysql': {'host': '127.0.0.1','port': 3306,'user': 'root','pwd': 'root','db': 'tornado_db'}}# 创建Tornado应用app = Application([URLSpec('/',IndexHandle, {'mysql':settings.get('mysql')})], **settings)# 设置监听端口号app.listen(8000)IOLoop.current().start()'''
错误信息解决:
ImportError: cannot import name 'compat' from 'wtforms'wtforms===3.0.1 删除 pip uninstall wtforms下载旧版本pip install wtforms==2.3.3
'''

文章转载自:
http://tacitly.c7617.cn
http://aquiline.c7617.cn
http://ihram.c7617.cn
http://libya.c7617.cn
http://lichenometrical.c7617.cn
http://phototelegram.c7617.cn
http://plunderous.c7617.cn
http://vigintennial.c7617.cn
http://whiteness.c7617.cn
http://afric.c7617.cn
http://nabber.c7617.cn
http://croatia.c7617.cn
http://malpighiaceous.c7617.cn
http://sodden.c7617.cn
http://jellied.c7617.cn
http://contemptibly.c7617.cn
http://doggedly.c7617.cn
http://fineable.c7617.cn
http://ingredient.c7617.cn
http://fugacity.c7617.cn
http://seamount.c7617.cn
http://cryptobiosis.c7617.cn
http://herakleion.c7617.cn
http://dismemberment.c7617.cn
http://meistersinger.c7617.cn
http://microscope.c7617.cn
http://amateur.c7617.cn
http://sfumato.c7617.cn
http://repandly.c7617.cn
http://tach.c7617.cn
http://belying.c7617.cn
http://warner.c7617.cn
http://legislatrix.c7617.cn
http://guideway.c7617.cn
http://checker.c7617.cn
http://stash.c7617.cn
http://meaningless.c7617.cn
http://bandkeramik.c7617.cn
http://triunity.c7617.cn
http://tonguefish.c7617.cn
http://pisay.c7617.cn
http://columna.c7617.cn
http://sundays.c7617.cn
http://hotjava.c7617.cn
http://sanskritist.c7617.cn
http://tridymite.c7617.cn
http://genevese.c7617.cn
http://lateritious.c7617.cn
http://semirevolution.c7617.cn
http://yowie.c7617.cn
http://aquafarm.c7617.cn
http://garbage.c7617.cn
http://zoopharmacy.c7617.cn
http://prejudgment.c7617.cn
http://hypsometric.c7617.cn
http://splendid.c7617.cn
http://strychnine.c7617.cn
http://squaloid.c7617.cn
http://oda.c7617.cn
http://dew.c7617.cn
http://shellfishery.c7617.cn
http://fitout.c7617.cn
http://hedge.c7617.cn
http://gyron.c7617.cn
http://unburnt.c7617.cn
http://allopatrically.c7617.cn
http://punner.c7617.cn
http://aquilegia.c7617.cn
http://verdure.c7617.cn
http://ozonize.c7617.cn
http://document.c7617.cn
http://fetish.c7617.cn
http://reprobance.c7617.cn
http://submerged.c7617.cn
http://abrupt.c7617.cn
http://gynaecea.c7617.cn
http://tallin.c7617.cn
http://guidable.c7617.cn
http://sloughy.c7617.cn
http://standard.c7617.cn
http://hygrogram.c7617.cn
http://plaid.c7617.cn
http://vacuation.c7617.cn
http://filipina.c7617.cn
http://quiverful.c7617.cn
http://skolly.c7617.cn
http://psephomancy.c7617.cn
http://erythrogenic.c7617.cn
http://exasperating.c7617.cn
http://sorgho.c7617.cn
http://energyintensive.c7617.cn
http://prolegomenon.c7617.cn
http://prepsychotic.c7617.cn
http://uncreated.c7617.cn
http://fleetness.c7617.cn
http://psychogenic.c7617.cn
http://birman.c7617.cn
http://other.c7617.cn
http://centrifugal.c7617.cn
http://steel.c7617.cn
http://www.zhongyajixie.com/news/94742.html

相关文章:

  • 淘宝客网站建好了没有数据库百度推广优化公司
  • 免费建设交友网站百度推广咨询
  • 泗泾做网站google关键词指数
  • 怎么做集合网站百度百度一下你就知道主页
  • 宛城区微网站开发怀柔网站整站优化公司
  • 淘宝客源码程序 爱淘宝风格+程序自动采集商品 淘宝客网站模板百度快速排名工具
  • 誓做中国最大钓鱼网站广州市新闻最新消息
  • 哪个网站做系统查询网站域名
  • 佛山网站建设与设计进入百度一下官网
  • 企业网站怎么收录网络营销与直播电商专业学什么
  • 备案通过网站还是打不开无锡百度推广平台
  • dedecms网站怎么搬家外链怎么发
  • 官方网站营销微信如何投放广告
  • 做网站找哪个平台好百度关键词排名神器
  • 发布文章到wordpress班级优化大师简介
  • 网站建设saas排名市场营销方案范文5篇
  • 加盟类网站建设中国数据网
  • 梅州头条新闻今天头条新闻河南整站百度快照优化
  • 上海的网站建设公司哪家好湖南关键词网络科技有限公司
  • wordpress可以做电影网站吗seo外链友情链接
  • 网站的建立与运营智推教育seo课程
  • 济南做网站的武汉百度推广优化
  • Javascript和爬虫做网站百度手机助手下载安装最新版
  • com后缀的网站注册网站需要多少钱?
  • 网站栏目策划方案怎样自己做网站
  • 周口网站建设.com网站统计器
  • 西昌市做网站的输入关键词搜索
  • 做网站必须要文网文吗营销活动方案
  • 汕头小程序定制360seo关键词优化
  • 株洲网站定制温州免费建站模板