9-tornado-Template优化方法、个人信息案例、tornado中ORM的使用(peewee的使用、peewee_async)、WTForms的使用

这篇具有很好参考价值的文章主要介绍了9-tornado-Template优化方法、个人信息案例、tornado中ORM的使用(peewee的使用、peewee_async)、WTForms的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在很多情况下,前端模板中在很多页面有都重复的内容可以使用,比如页头、页尾、甚至中间的内容都有可能重复。

这时,为了提高开发效率,我们就可以考虑在共同的部分提取出来,
主要方法有如下:
1. 模板继承
2. UI模板

1 模板继承

common/base.html

{% block content %}
{% end %}

shop2.html

{% extends 'common/base.html'%}

{% block content %}
{% end %}

2 UI模板

Tornado中支持累死Vue中的组件功能,就是也公共的内容提取出来当成组件。
具体的使用方式是用tornado.web.UIModule

class Entry(tornado.web.UIModule):
    def render(self, entry, show_comments=False):
        return self.render_string(
            "module-entry.html", entry=entry, show_comments=show_comments)
            
settings = {
    "Entry": Entry,
}

3 个人信息案例

环境搭建

在网站中,少了数据CRUD的操作,但在tornado中,我们知道若想操作的话尽量使用异步的操作,这样效率才会高。

那应该如何编写呢,下面我们来一起做下吧。首先我们先一起搭建下环境。

具体的操作如下:

前端

copy 原生素材

html —> templates

css —> static/css

js —> static/js

img —> static/img

到项目中

后端

from tornado import web, ioloop
from tornado.web import StaticFileHandler

class IndexHandler(web.RequestHandler):
    def get(self):
        self.render('personal.html')

import os
base_url = os.path.dirname(os.path.abspath(__file__))
settings={
    'static_path':os.path.join(base_url,'static'),
    'static_url_prefix':'/static/',
    'template_path':os.path.join(base_url,'templates')
}
if __name__ == "__main__":
    app = web.Application([
        web.URLSpec('/',IndexHandler,name='index'),
    ],
    debug=True,
    **settings
    )
    app.listen(8000)
    ioloop.IOLoop.current().start()

MySQL

CREATE TABLE `tornado_demo1`.`t_user`  (
  `id` int(10) NOT NULL,
  `username` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `nick_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `email` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `passsword` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `phone` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `language` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

熟悉Mysql操作

之前我们了解过pymysql操作数据库,但那是同步的。

我们需要使用一个异步的框架,在这,我们推荐使用aiomysql,它的底层大量的使用了pymysql,只是它通过asyncio实现的访问数据库

安装方式

pip install aiomysql

使用方式

import asyncio
import aiomysql

async def test_example(loop):
    pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
                                      user='root', password='root',
                                      db='mysql', loop=loop)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 42;")
            print(cur.description)
            (r,) = await cur.fetchone()
            assert r == 42
    pool.close()
    await pool.wait_closed()


loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))

4 tornado中ORM的使用

4.1 peewee的使用

官网链接

Peewee是一个简单而小型的ORM。使其易于学习且使用直观。

  • 一个小的,表达力很强的ORM
  • python 2.7+和3.4+(使用3.11开发)
  • 支持sqlite,mysql,postgresql和cockroachdb
  • 大量的扩展

安装

pip install peewee
 tip
需要安装pymysql,不然会报错

peewee.ImproperlyConfigured: MySQL driver not installed!

### 使用方式 

#### 创建表


from peewee import *

db =  MySQLDatabase('message', host="127.0.0.1", port=3306, user="root", password="root")

class BaseModel(Model):
    create_time = DateTimeField(default=datetime.now, verbose_name="添加时间")

class Music(BaseModel):
    name = CharField(index=True)
    singer = CharField(max_length=11, verbose_name="演唱者")
    duration = CharField(max_length=11, verbose_name="时长")
    _type = CharField(max_length =11,verbose_name="音乐类型")
    commany = ForeignKeyField(Commany,verbose_name="版权",backref = "musics")
    class Meta:
        database = db 
        table_name = 't_music'

class Commany(BaseModel):
    name = CharField()
    address = CharField()
    year - CharField()
db.create_tables([Cmmany,Music])

增加数据

    c = Commany()
    c.name = '中国人'
    c.full_name = '北京'
    c.year = 2003
    c.save()

    print(c.id)

    m = Music(name='中国', singer='中国1', duration='1:50',
              _type='流行', commany=c.id)
    m.save()

案例数据

    commanys = [
            {
                'name': '滚石唱片',
                'full_name': '滚石国际音乐股份有限公司',
                'year': 1980
            },
            {
                'name': '华谊兄弟',
                'full_name': '华谊兄弟传媒股份有限公司',
                'year': 1994
            },
            {
                'name': '海蝶音乐',
                'full_name': '北京太合音乐文化发展有限公司',
                'year': 1986
            },
        ]
        musics = [
            {
                "name": "你是我左边的风景",
                "singer": "林志炫",
                "duration": "2:20",
                "_type": "摇滚",
                "commany": 1
            },
            {
                "name": "把你揉碎捏成苹果",
                "singer": "薛之谦",
                "duration": "2:10",
                "_type": "摇滚",
                "commany": 3
            },
            {
                "name": "游戏人间",
                "singer": "童安格",
                "duration": "1:20",
                "_type": "流行",
                "commany": 2
            },
            {
                "name": "故乡的云",
                "singer": "费翔",
                "duration": "2:40",
                "_type": "摇滚",
                "commany": 1
            },
            {
                "name": "诺言Jason",
                "singer": "青城山下白素贞",
                "duration": "1:10",
                "_type": "古典",
                "commany": 3
            },
            {
                "name": "勇敢的幸福",
                "singer": "Sweety",
                "duration": "1:23",
                "_type": "古典",
                "commany": 2
            },
            {
                "name": "爱丫爱丫",
                "singer": "By2",
                "duration": "2:22",
                "_type": "流行",
                "commany": 1
            },
            {
                "name": "我也曾像你一样",
                "singer": "马天宇",
                "duration": "2:28",
                "_type": "流行",
                "commany": 1
            }
        ]
    
    #### 查询数据
   
    The following types of comparisons are supported by peewee:
    
    | Comparison | Meaning                                 |
    | ---------- | --------------------------------------- |
    | `==`       | x equals y                              |
    | `<`        | x is less than y                        |
    | `<=`       | x is less than or equal to y            |
    | `>`        | x is greater than y                     |
    | `>=`       | x is greater than or equal to y         |
    | `!=`       | x is not equal to y                     |
    | `<<`       | x IN y, where y is a list or query      |
    | `>>`       | x IS y, where y is None/NULL            |
    | `%`        | x LIKE y where y may contain wildcards  |
    | `**`       | x ILIKE y where y may contain wildcards |
    | `^`        | x XOR y                                 |
    | `~`        | Unary negation (e.g., NOT x)            |
    
    Because I ran out of operators to override, there are some additional query operations available as methods:
    
    | Method                | Meaning                                         |
    | --------------------- | ----------------------------------------------- |
    | `.in_(value)`         | IN lookup (identical to `<<`).                  |
    | `.not_in(value)`      | NOT IN lookup.                                  |
    | `.is_null(is_null)`   | IS NULL or IS NOT NULL. Accepts boolean param.  |
    | `.contains(substr)`   | Wild-card search for substring.                 |
    | `.startswith(prefix)` | Search for values beginning with `prefix`.      |
    | `.endswith(suffix)`   | Search for values ending with `suffix`.         |
    | `.between(low, high)` | Search for values between `low` and `high`.     |
    | `.regexp(exp)`        | Regular expression match (case-sensitive).      |
    | `.iregexp(exp)`       | Regular expression match (case-insensitive).    |
    | `.bin_and(value)`     | Binary AND.                                     |
    | `.bin_or(value)`      | Binary OR.                                      |
    | `.concat(other)`      | Concatenate two strings or objects using `||`.  |
    | `.distinct()`         | Mark column for DISTINCT selection.             |
    | `.collate(collation)` | Specify column with the given collation.        |
    | `.cast(type)`         | Cast the value of the column to the given type. |
    
    To combine clauses using logical operators, use:
    
    | Operator   | Meaning              | Example                                              |
    | ---------- | -------------------- | ---------------------------------------------------- |
    | `&`        | AND                  | `(User.is_active == True) & (User.is_admin == True)` |
    | `\|` (pipe) | OR                   | `(User.is_admin) \| (User.is_superuser)`              |
    | `~`        | NOT (unary negation) | `~(User.username.contains('admin'))`                 |
    
    Here is how you might use some of these query operators:



# Find the user whose username is "charlie".
User.select().where(User.username == 'charlie')

# Find the users whose username is in [charlie, huey, mickey]
User.select().where(User.username.in_(['charlie', 'huey', 'mickey']))

Employee.select().where(Employee.salary.between(50000, 60000))

Employee.select().where(Employee.name.startswith('C'))

Blog.select().where(Blog.title.contains(search_string))

更新数据

def update_music():
    # 方法1:获取数据,在对象上直接修改
    # m = Music.get_by_id(1)
    # m.singer='林志炫666'
    # m.save()

    # 方法2:利用类方法
    # update table set ??? where ???
    Music.update(singer='林志炫').where(Music.id == 1).execute()

删除数据

def delete_music():
    # 方法1:直接删除对象
    # m = Music.get_by_id(8)
    # m.delete_instance()

    # 方法2:利用类方法
    Music.delete().where(Music.id >5).execute()

4.2 peewee_async

peewee_async

安装

PostgreSQL

pip install --pre peewee-async
pip install aiopg

MySQL

pip install --pre peewee-async
pip install aiomysql

案例

import asyncio
import peewee
import peewee_async

# Nothing special, just define model and database:

database = peewee_async.PostgresqlDatabase(
    database='db_name',
    user='user',
    host='127.0.0.1',
    port='5432',
    password='password'
)

class TestModel(peewee.Model):
    text = peewee.CharField()

    class Meta:
        database = database

# Look, sync code is working!

TestModel.create_table(True)
TestModel.create(text="Yo, I can do it sync!")
database.close()

# Create async models manager:

objects = peewee_async.Manager(database)

# No need for sync anymore!

database.set_allow_sync(False)

async def handler():
    await objects.create(TestModel, text="Not bad. Watch this, I'm async!")
    all_objects = await objects.execute(TestModel.select())
    for obj in all_objects:
        print(obj.text)

loop = asyncio.get_event_loop()
loop.run_until_complete(handler())
loop.close()

# Clean up, can do it sync again:
with objects.allow_sync():
    TestModel.drop_table(True)

# Expected output:
# Yo, I can do it sync!
# Not bad. Watch this, I'm async!

5 WTForms的使用

简介

WTForms是用于Python Web开发的灵活的表单验证和呈现库。它可以与您选择的任何Web框架和模板引擎一起使用。

WTForms文档
WTForms_Tornado

安装

pip install wtforms-tornado

案例

import tornado.ioloop
import tornado.web

from wtforms.fields import IntegerField
from wtforms.validators import Required
from wtforms_tornado import Form

class SumForm(Form):

    a = IntegerField(validators=[Required()])
    b = IntegerField(validators=[Required()])

class SumHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

    def post(self):
        form = SumForm(self.request.arguments)
        if form.validate():
            self.write(str(form.data['a'] + form.data['b']))
        else:
            self.set_status(400)
            self.write("" % form.errors)

application = tornado.web.Application([
    (r"/", SumHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

表单验证

wftform.py

from wtforms.fields import IntegerField,StringField
from wtforms_tornado import Form
from wtforms.validators import DataRequired, Length

class UserForm(Form):
    id = IntegerField('ID')
    username  = StringField('用户名',validators=[DataRequired(message='请输入用户名'),Length(min=3,max=8,message='请输入3-8长度的用户名')])
    nick_name = StringField('昵称')
    email = StringField('Email')
    password = StringField('密码')
    phone = StringField('手机号')
    language = StringField('语言')

handler.py文章来源地址https://www.toymoban.com/news/detail-757084.html

forms = UserForm(self.request.arguments)
if forms.validate():
    del forms.data['id']
    await objs.create(User,**forms.data)
    self.render('personal26.html',user_form = forms)
else:
    self.render('personal26.html',user_form = forms)

HTML生成

{% autoescape None %}
	{% for field in user_form %}
   	 	{% if field.label.text == "ID"%}
	        <div class="form-group">
	            {{ field(class_="au-input au-input--full",placeholder=field.label.text)}}
	        </div>
    	{% else %}
	        <div class="form-group">
	            {{ field.label }}
	            {{ field(class_="au-input au-input--full",placeholder=field.label.text)}}
	            {% if field.errors %}
	                {{field.errors}}
	            {% end %}

	        </div>
    {% end %}                            
{% end %}

到了这里,关于9-tornado-Template优化方法、个人信息案例、tornado中ORM的使用(peewee的使用、peewee_async)、WTForms的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • R语言的水文、水环境模型优化技术及快速率定方法与多模型案例实践

    在水利、环境、生态、机械以及航天等领域中,数学模型已经成为一种常用的技术手段。同时,为了提高模型的性能,减小模型误用带来的风险;模型的优化技术也被广泛用于模型的使用过程。模型参数的快速优化技术不但涉及到优化本身而且涉及采样设计、不确定性估计等

    2024年02月16日
    浏览(26)
  • Kotlin~Template模版方法模式

    概念 定义算法骨架、代码模版 角色介绍 Abstract Class Concrete Class UML 代码实现 优缺点 优点 代码复用 统一代码框架 封装不变,扩展可变 缺点 增加复杂性 父类(基类)改动影响大 应用场景 一次性实现算法的不变部分,集中公共行避免代码重复。 数据库访问,Spring持久层处理

    2024年02月12日
    浏览(29)
  • 模板方法模式(Template Method)

    模板方法模式是一种行为设计模式,在超类中定义了一个算法的框架,而将一些步骤的实现延迟到子类中,使得子类可重定义该算法的特定步骤。 模板方法模式包含如下角色: Abstract ­Class,抽象类,实现一个模板方法,定义了算法的骨架。需要说明的是,算法中的步骤可以

    2024年02月14日
    浏览(90)
  • 用python怎么输出个人信息,python怎么输出个人信息

    大家好,给大家分享一下python输入自己的姓名,输出hello,某某某同学,很多人还不知道这一点。下面详细解释一下。现在让我们来看看! name = input(\\\"请输入姓名:\\\") print(\\\"你好,\\\",name) 代码: #include #include #include #define MAX_LENGTH 50 int main() { char cFirstName[MAX_LENGTH]; char cLastName[MAX_LENG

    2024年02月08日
    浏览(24)
  • 【设计模式】模板方法模式(Template Method Pattern)

    模板方法模式是一种行为型设计模式,它定义了一个算法骨架,将某些算法步骤的实现延迟到子类中。 这样可以使得算法的框架不被修改,但是具体的实现可以根据需要进行调整。 在模板方法模式中,我们通常会定义 一个抽象类 ,它包含了 一个模板方法 和 一些抽象方法

    2024年02月12日
    浏览(30)
  • 设计模式-8--模板方法模式(Template Method Pattern)

    模板方法模式(Template Method Pattern)是一种行为型设计模式,它定义了一个算法的骨架,将一些步骤的实现延迟到子类中。模板方法模式允许在不改变算法的结构的情况下,通过在子类中重写特定步骤的具体实现,来改变算法的部分行为。 模板方法模式通常包括以下几个角色

    2024年02月09日
    浏览(26)
  • 《社区人员管理》实战案例设计&个人案例分享

    作者:京东科技 刘宗恺 北京市疫情防控一体化平台是以“京办”为支撑,集病例流调、排查管控、隔离管理、人员转运、监测评估于一体的疫情防控平台,秉承五位一体统筹、现有体系融通、共性能力支撑、高效部门系统、快速局部突破的理念,进行建设。 功能:多系统协

    2023年04月19日
    浏览(34)
  • 设计模式二十三:模板方法模式(Template Method Pattern)

    定义了一个算法的框架,将算法的具体步骤延迟到子类中实现。这样可以在不改变算法结构的情况下,允许子类重写算法的特定步骤以满足自己的需求 模版方法使用场景 算法框架固定,但具体步骤可以变化:当你有一个算法的整体结构是固定的,但其中某些步骤的实现可能

    2024年02月11日
    浏览(26)
  • 个人信息流转安全合规探索

    目录 0 引言 1 个人信息流转 1.1 个人信息流转的分类 图1 图2 图3 图4 图5 图6 1.2 易混淆的概念 1.2.1 委托处理与第三方接入 1.2.2 共享与共同控制 1.3 小结 2 个人信息流转安全合规实践问题及原因分析 2.1 产业界、法规界、标准界认知的差异 2.2 个人信息流转的模糊边界 2.3 个人信

    2024年04月13日
    浏览(22)
  • 微信小程序个人中心页面 案例

    微信小程序 开发,经常会遇到个人中心页面 的需求,为了方便大家使用,决定将个人总想页面进行开源,以供大家参考交流。 一、效果预览        二、源代码 abouthe.json文件 abouthe.wxml文件 abouthe.wxss文件 abouthe.ts文件

    2024年02月11日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包