web框架--tornado自定义分页
1、tornado_main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- import tornado.web
import tornado.ioloop LIST_INFO = [
{'username': 'yusheng', 'email': '137@163.com'}
]
for i in range(200):
temp = {'username': str(i) + "lys", 'email': str(i) + "@163.com"}
LIST_INFO.append(temp) class Pagenation: def __init__(self, current_page, all_item, base_url): #当前页 内容总数 目录
try:
page = int(current_page)
except:
page = 1
if page < 1:
page = 1 all_page, c = divmod(all_item, 5)
if c > 0:
all_page += 1 self.current_page = page
self.all_page = all_page
self.base_url = base_url @property
def start(self):
return (self.current_page - 1) * 5 @property
def end(self):
return self.current_page * 5 def string_pager(self):
list_page = []
if self.all_page < 11:
s = 1
t = self.all_page + 1
else:
if self.current_page < 6:
s = 1
t = 12
else:
if (self.current_page + 5) < self.all_page:
s = self.current_page-5
t = self.current_page + 6
else:
s = self.all_page - 11
t = self.all_page +1 first = '<a href = "/index/1">首页</a>'
list_page.append(first)
# 当前页
if self.current_page == 1:
prev = '<a href = "javascript:void(0):">上一页</a>'
else:
prev = '<a href = "/index/%s">上一页</a>'%(self.current_page-1,)
list_page.append(prev) #页码
for p in range(s, t):
if p== self.current_page:
temp = '<a class = "active" href = "/index/%s">%s</a>' % (p, p)
else:
temp = '<a href = "/index/%s">%s</a>' % (p, p)
list_page.append(temp) # 尾页
if self.current_page == self.all_page:
nex = '<a href = "javascript:void(0):">下一页</a>'
else:
nex = '<a href = "/index/%s">下一页</a>' % (self.current_page + 1,)
list_page.append(nex) last = '<a href = "/index/%s">尾页</a>' % (self.all_page)
list_page.append(last) #跳转
jump = '''<input type="text"><a onclick = "Jump('%s',this);">GO</a>''' % ('/index/')
script = '''
<script>
function Jump(baseUrl,ths){
var val = ths.previousElementSibling.value;
if (val.trim().length > 0){
location.href = baseUrl + val;
}
}
</script>
'''
list_page.append(jump)
list_page.append(script)
str_page = "".join(list_page) return str_page class IndexHandler(tornado.web.RequestHandler): def get(self, page):
obj = Pagenation(page, len(LIST_INFO), '/index/')
current_list = LIST_INFO[obj.start:obj.end]
str_page = obj.string_pager()
self.render('index.html', list_info=current_list, current_page=obj.current_page, str_page=str_page) application = tornado.web.Application([
(r'/index/(?P<page>\d*)', IndexHandler) ]) if __name__ == "__main__":
application.listen(8080)
tornado.ioloop.IOLoop.instance().start()
2、index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pager a{
display: inline-block;
padding: 5px 6px;
margin: 10px 3px;
border: 1px solid #2b669a;
text-decoration:none; }
.pager a.active{
background-color: #2b669a;
color: white;
}
</style>
</head>
<body>
<h3>显示数据</h3>
<table border="">
<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>
<div class="pager">
{% raw str_page %}
</div>
</body>
</html>
3、图示

web框架--tornado自定义分页的更多相关文章
- Python Web框架 tornado 异步原理
Python Web框架 tornado 异步原理 参考:http://www.jb51.net/article/64747.htm 待整理
- 第三百一十四节,Django框架,自定义分页
第三百一十四节,Django框架,自定义分页 自定义分页模块 #!/usr/bin/env python #coding:utf-8 from django.utils.safestring impo ...
- Python web框架 Tornado异步非阻塞
Python web框架 Tornado异步非阻塞 异步非阻塞 阻塞式:(适用于所有框架,Django,Flask,Tornado,Bottle) 一个请求到来未处理完成,后续一直等待 解决方案: ...
- Python web框架 Tornado(三)自定义session组件
我们在学习Django框架的过程中,内部封装了session组件,以方便于我们使用进行验证.但是Tornado框架是没有session的,所以如果想使用session的话,就需要我们自己定制相对应的组 ...
- 第一个web框架tornado
简介 tornado,是我学到的第一个web框架是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google ...
- Python web框架 Tornado(一)基础学习
概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
- web框架--tornado框架之模板引擎
使用Tornado实现一个简陋的任务表功能demo来讲解tornado框架模板引擎 一.demo目录结构 二.具体文件内容 2.1.commons.css .body{ margin: 0; back ...
- 异步非阻塞IO的Python Web框架--Tornado
Tornado的全称是Torado Web Server,从名字上就可知它可用作Web服务器,但同时它也是一个Python Web的开发框架.最初是在FriendFeed公司的网站上使用,FaceBo ...
- Python web框架——Tornado
Tornado是一个Python Web框架和异步网络库,最初由FriendFeed开发.通过使用非阻塞网络I / O,Tornado可以扩展到数万个开放连接,使其成为需要长时间连接每个用户的长轮询, ...
随机推荐
- node爬虫之图片下载
背景:针对一些想换头像的玩家,而又不知道用什么头像的,作为一名代码爱好者,能用程序解决的,就不用程序来换头像,说干就干,然后就整理了一下. 效果图 环境配置 安装node环境 node -v node ...
- cURL error 60: SSL certificate problem: unable to get local issuer certificate(转)【亲测】
php5.6以上的版本会出现这种问题 解决办法: [开启拓展] extension=curl extension=openssl [配置证书] 访问https://curl.haxx.se/docs/ ...
- ubuntu / zsh shell / oh-my-zsh / 常用插件
记录一下 zsh 的下载与配置,省得每次重装系统都要上网到处查. 安装 zsh shell sudo apt install zsh 切换 shell chsh -s /bin/zsh 安装 oh-m ...
- Redhat6.6替换Centos Yum源
1.删除当前系统自带的yum [root@reddhat6_155_201 ~]# rpm -qa |grep yum yum-rhn-plugin--.el6.noarch yum-utils--. ...
- Linux安装centos,网络net8模式ping不通www.baidu.com或者ping不通主机
1.Linux安装centos,网络net8模式ping不通www.baidu.com或者ping不通主机. 我使用的是net8模式.配置如下所示,保证可以ping通www.baidu.com或者pi ...
- C# 灵活切换开发/测试/生成环境web.config
web.config <configuration> <connectionStrings configSource="config\Sit.db.config" ...
- Linux下java验证码不显示:Could not initialize class sun.awt.X11FontManager
一.问题 javaweb项目,登录的时候有个图片验证码的功能.在Windows本地测试能够正常显示,部署到Linux上就不行了.报错如下: org.springframework.web.util.N ...
- 将多个sass文件合并到一个文件中
将多个sass文件合并到一个文件中 应用场景:制作angular npm包的时候,定义的一些全局样式,自定义主题色这类的情况下,多个scss文件会要合并成一个文件并写到dist文件里,发布到仓库中. ...
- 「你学习,我买单」i春秋四周年精品课程福利专场
i春秋:中国专业的网络安全在线教育平台,累计用户超过60万. i春秋课程:资深专家团队教研支持,人才培养结合企业需求,针对不同岗位和技术方向的课程总计:600+门,1500+章,5700+节,时长74 ...
- 在 Docker 中已运行的 container 如何修改 run 时的 env
https://www.cnblogs.com/xiaouisme/p/9837221.html 首先不推荐这样做,如需修改配置,应删掉重新部署. 其次,可以进行如下操作(未测试,不知道仅重启 con ...