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可以扩展到数万个开放连接,使其成为需要长时间连接每个用户的长轮询, ...
随机推荐
- Introduction to Semidefinite Programming (SDP)
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-251j-introduction-to-mathe ...
- 关于XSS攻击
1.XSS XSS(Cross Site Scripting)攻击全称跨站脚本攻击,为了不与CSS(Cascading Style Sheets)混淆,故将跨站脚本攻击缩写为XSS,XSS是一种经常出 ...
- OpenDaylight开发hello-world项目之开发环境搭建
OpenDaylight开发hello-world项目之开发环境搭建 OpenDaylight开发hello-world项目之开发工具安装 OpenDaylight开发hello-world项目之代码 ...
- [2019BUAA软工助教]下半学期改进计划
[2019BUAA软工助教]下半学期改进计划 结合[2019BUAA软工助教]答黄衫同学,经过26日晚陈彦吉.刘畅.赵奕.李庆想四位助教的讨论,最终整理了以下这份计划 一.技术博客 各个团队在开发的过 ...
- PM学习笔记(一):解构产品经理
1.产品定义:什么是产品 来自百度百科(链接)的解释: 产品是指能够供给市场 [1] ,被人们使用和消费,并能满足人们某种需求的任何东西,包括有形的物品.无形的服务.组织.观念或它们的 ...
- AutoDesk公司搞的fbx模型格式
FBX® data exchange technology is a 3D asset exchange format that facilitates higher-fidelity data ex ...
- redis之主从同步
很多企业都没有使用到 Redis 的集群,但是至少都做了主从.有了主从,当 master 挂掉的时候,运维让从库过来接管,服务就可以继续,否则 master 需要经过数据恢复和重启的过程,这就可能会拖 ...
- 微软开放了.NET 4.5.1的源代码【转】
.NET Reference Source发布了beta版,可以在线浏览.NET Framework 4.5.1的源代码,并且可以通过配置,在Visual Studio 2013中调试.NET Fra ...
- 场sharepoint2016数据库恢复站点
前不久公司support方,不小心把IIS的应用删除了,算是灼急了,不过有过原来恢复的经历,似乎有了心理准备,可是这次比上次严重些.技术操作复杂些,不过通过此事,也是进一步了解了SP2016数据库结构 ...
- linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行
linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行 crontab每分钟执行一次,但5秒以上才有更新数据,有时候一分钟可能跑不完上一个进程,需要先等10秒再判 ...