[py]tornado分页实现-从本质到完全实现

这篇博客详细介绍了如何在Tornado框架中实现分页功能,从项目的初始化到分页链接的高亮显示,包括处理分页异常、动态生成分页链接、防止XSS攻击等关键步骤,同时提供了相应的代码示例。
摘要由CSDN通过智能技术生成

分页程序git代码

初始化tornado目录

构建项目–实现post提交,get展示数据

实现类似

代码逻辑

完整代码

start.py

#!/usr/bin/env python
# coding=utf-8
import time
import tornado.ioloop
import tornado.web


# 业务逻辑处理模块


# 配置选项模块
from controllers import home


# 静态文件和模板文件的配置
settings = {
    'template_path': 'templates',
    'static_path': 'statics',
}

# 路由模块
## 动态路由系统
application = tornado.web.Application([
    (r"/index/(?P<page>\d+)/(?P<nid>\d+)", home.IndexHandler),
],
    **settings
)

## wsgi模块
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
controllers/home.py

#!/usr/bin/env python
# coding=utf-8
import tornado.web

LIST_INFO = [
    {
  "username": "maotai", "email": "123456"},
]


class IndexHandler(tornado.web.RequestHandler):
    # def get(self, *args, **kwargs):
    def get(self,page,nid):
        # print(page,nid)
        self.render("home/index.html", list_info=LIST_INFO)

    def post(self, *args, **kwargs):
        username = self.get_argument("username", "")
        email = self.get_argument("email", "")

        tmp = {
  "username": username, "email": email}
        LIST_INFO.append(tmp)
        self.redirect("/index/1/1")
home/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>添加数据</h1>
<form action="/index/1/1" method="post">
    <input type="text" name="username">
    <input type="email" name="email">
    <input type="submit" typeof="提交">
</form>
<h1>显示数据</h1>
<table border="1">
    <thead>
        <tr>
            <th>用户名</th>
            <th>邮箱</th>
        </tr>
    </thead>
    <tbody>
        {% for line in list_info %}
            <tr>
                <td>{
   { line['username'] }}</td>
                <td>{
   { line['email'] }}</td>
            </tr>
        {% end %}
    </tbody>
</table>
</body>
</html>

实现url //

实现分页-每页显示5条数据

效果:

代码逻辑:

完整代码:

#!/usr/bin/env python
# coding=utf-8
import time
import tornado.ioloop
import tornado.web


# 业务逻辑处理模块


# 配置选项模块
from controllers import home


# 静态文件和模板文件的配置
settings = {
    'template_path': 'templates',
    'static_path': 'statics',
}

# 路由模块
## 动态路由系统
application = tornado.web.Application([
    (r"/index/(?P<page>\d*)", home.IndexHandler),
],
    **settings
)

## wsgi模块
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python
# coding=utf-8
import tornado.web

LIST_INFO = [
    {
  "username": "maotai", "email": "123456"},
]


class IndexHandler(tornado.web.RequestHandler):
    # def get(self, *args, **kwargs):
    def get(self, page):
        # 每页显示5条数据
        # LIST_INFO[0:5]
        # LIST_INFO[5:10]
        page = int(page)
        print(page)
        start = (page - 1) * 5
        end = page * 5
        current_list = LIST_INFO[start:end]
        self.render("home/index.html", list_info=current_list)

    def post(self, *args, **kwargs):
        username = self.get_argument("username", "")
        email = self.get_argument("email", "")

        tmp = {
  "username": username, "email": email}
        LIST_INFO.append(tmp)
        self.redirect("/index/1")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>添加数据</h1>
<form action="/index/1" method="post">
    <input type="text" name="username">
    <input type="email" name="email">
    <input type="submit" typeof="提交">
</form>
<h1>显示数据</h1>
<table border="1">
    <thead>
        <tr>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值