python实现分页插件
class Pages:
def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):#pager_num 这是页码
self.current_page = current_page
self.data_count = data_count
self.per_page_count = per_page_count
self.pager_num = pager_num
pass @property
def start(self):
return (self.current_page-1) * self.per_page_count @property
def end(self):
return self.current_page * self.per_page_count @property
def all_count(self):
v, s = divmod(self.data_count, self.per_page_count)
if s > 0:
v += 1
return v def page_str(self, base_url):
pg = []
if self.all_count < self.pager_num:
start_index = 1
ent_index = self.all_count
else:
if self.current_page < (self.pager_num + 1) / 2: # 最前面的数,当小于6时会出现的一种情况
start_index = 1
ent_index = self.pager_num
elif self.current_page + 5 > self.all_count: # 最后面的数,当加上5大于总页数时会出现的一种情况
start_index = self.all_count - self.pager_num + 1
ent_index = self.all_count
else:
start_index = self.current_page - ((self.pager_num - 1) / 2)
ent_index = self.current_page + ((self.pager_num - 1) / 2) pre_page = "<a href='%s?p=%s' class='page'>上一页</a>" % \
(base_url, self.current_page - 1 if self.current_page > 1 else 1) for a in range(int(start_index), int(ent_index)+1):
if a == self.current_page:
temp = "<a href='%s?p=%s' class='page active'>%s</a>" % (base_url, a, a)
pg.append(temp)
else:
temp = "<a href='%s?p=%s' class='page'>%s</a>" % (base_url, a, a)
pg.append(temp) next_page = "<a href='%s?p=%s' class='page'>下一页</a>" % \
(base_url, self.current_page + 1 if self.current_page < self.all_count else self.all_count) jump = """
<input id="jump" type="text"><button onclick="jump(this)">跳转</button>
<script>
function jump(node){
location.href= '%s?p='+ $("#jump").val()
}
</script>
""" % base_url
page_str = pre_page+"".join(pg)+next_page+jump
return page_str
使用例子:
def show_vedio(request):
current_page = request.GET.get("p", 1)
current_page = int(current_page)
page_obj = Pages(current_page, len(LIST))
lists = LIST[page_obj.start:page_obj.end] return render(request, "blogs/internet/show_video.html", {"list": lists, "page": page_obj.page_str("/blogs/video/")})
前端:
{{ "分页 "|defined_filter:"您会做 了吗!" }}
<ul>
{% for i in list %}
<li>{{ i }}</li>
{% endfor %}
</ul>
{{ page|safe }}
python实现分页插件的更多相关文章
- Jquery前端分页插件pagination同步加载和异步加载
上一篇文章介绍了Jquery前端分页插件pagination的基本使用方法和使用案例,大致原理就是一次性加载所有的数据再分页.https://www.jianshu.com/p/a1b8b1db025 ...
- Jquery前端分页插件pagination使用
插件描述:JqueryPagination是一个轻量级的jquery分页插件.只需几个简单的配置就可以生成分页控件.并且支持ajax获取数据,自定义请求参数,提供多种方法,事件和回调函数,功能全面的分 ...
- Flask学习之旅--分页功能:分别使用 flask--pagination 和分页插件 layPage
一.前言 现在开发一个网站,分页是一个很常见的功能了,尤其是当数据达到一定量的时候,如果都显示在页面上,会造成页面过长而影响用户体验,除此之外,还可能出现加载过慢等问题.因此,分页就很有必要了. 分页 ...
- MVC如何使用开源分页插件shenniu.pager.js
最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...
- 分页插件--根据Bootstrap Paginator改写的js插件
刚刚出来实习,之前实习的公司有一个分页插件,和后端的数据字典约定好了的,基本上是看不到内部是怎么实现的,新公司是做WPF的,好像对于ASP.NET的东西不多,导师扔了一个小系统给我和另一个同事,指了两 ...
- [原创]jquery+css3打造一款ajax分页插件
最近公司的项目将好多分页改成了ajax的前台分页以前写的分页插件就不好用了,遂重写一个 支持IE6+,但没有动画效果如果没有硬需求,个人认为没必要多写js让动画在这些浏览器中实现css3的动画本来就是 ...
- 一个强大的jquery分页插件
点击这里查看效果 这个分页插件使用方便,引用keleyidivpager.js和keleyidivpager.css文件,然后在htm(或者php,aspx,jsp等)页面中对分页总数,参数名,前缀后 ...
- 【SSM 5】Mybatis分页插件的使用
一.添加maven依赖项 <span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency&g ...
- Mybatis分页插件
mybatis配置 <!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pageh ...
随机推荐
- Vue2.5开发去哪儿网App 第三章笔记 上
1. vue 生命周期函数 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 ...
- Android众说纷纭分辨率
Andoid最被人诟病的就是显示屏的各种不同尺寸和不同分辨率.由于Android厂商的纷繁多样,导致出现了不同尺寸和不同分辨率的手机,指示开发者需要兼容各种手机屏幕.本文想学习的就是Android的显 ...
- Vue笔记:使用 VS Code 断点调试
直接在 Chrome 的调试窗口中调试 Vue 代码有诸多不便, 好在 Visual Studio Code 中提供了 Debugger for Chrome 插件,能够通过配置直接在 VS Code ...
- spring boot实现ssm(1)功能
前面完成了ssm的整合, 整个过程可以说很繁杂, 各种配置, 很容易让人晕掉. 这里使用spring boot 的方式来实现ssm(1)中的功能. 一. 建项目 1. 使用 idea 来创建 spri ...
- linux 初始化工作环境
#!/bin/sh # # init workspace # /bin/svnserve -d -r /home/oracle/projects --listen-port= su - oracle ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- 基于GoogLeNet的不同花分类微调训练案例
import tensorflow as tf from tensorflow.contrib.slim import nets slim = tf.contrib.slim import numpy ...
- .5-浅析express源码之Router模块(1)-默认中间件
模块application已经完结,开始讲Router路由部分. 切入口仍然在application模块中,方法就是那个随处可见的lazyrouter. 基本上除了初始化init方法,其余的app.u ...
- [转]Reporting Services 中的身份验证类型
本文转自:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/cc281310%28v%3dsql.100%2 ...
- C#网络爬虫--多线程处理强化版
上次做了一个帮公司妹子做了爬虫,不是很精致,这次公司项目里要用到,于是有做了一番修改,功能添加了网址图片采集,下载,线程处理界面网址图片下载等. 说说思路:首相获取初始网址的所有内容 在初始网址采集图 ...