前段时间用django给游戏写管理后台,用到了里面的分页组件。看了一下竟然不支持分片,一股脑取出数据,然后分页。感觉太浪费资源,自己就写了一个支持分片的分页类。
先上代码:
-
#! /usr/bin/env python
-
#coding=utf-8
-
-
#-----------------------------------
-
# Name: Core_Lib_Page.py
-
# Desc: 分页封装类
-
# Author: Lunay Zhao 2013-12-14
-
#-----------------------------------
-
-
import math
-
import urllib
-
class Core_Lib_Page(object):
-
_pagebar_html = \
-
"""
-
-page">
-
-totlerecord">%d
-
-totlepages">%d/%d%s
-
"""
-
-
def __init__(self, url, page, count, perpage, params={}):
-
self._url = url
-
self._page = page
-
self._count = count
-
self._perpage = perpage
-
self._pagenum = (count / perpage + 1) if count % perpage > 0 else (count / perpage if count / perpage > 0 else 1)
-
self._params = params
-
self.pagebar = ""
-
-
def showPage(self):
-
self.pagebar = ""
-
#首页以及上一页
-
if self._page == 1:
-
self.pagebar += ''+u"首页"+''
-
self.pagebar += ''+u"上一页"+''
-
else:
-
#首页
-
self._params["page"] = 1
-
params_str = urllib.urlencode(self._params)
-
self.pagebar += '+self._url+'?'+params_str+'">' + u"首页" + ''
-
-
#上一页
-
self._params["page"] = self._page - 1
-
params_str = urllib.urlencode(self._params)
-
self.pagebar += '+self._url+'?'+params_str+'">' + u"上一页" + ''
-
-
if self._pagenum == 1:
-
self.pagebar += '1'
-
else:
-
if self._pagenum <= 10:
-
self.disposeUrl(1, self._pagenum+1)
-
else:
-
#继续判断条件
-
if self._pagenum - self._page <= 5:
-
self.disposeUrl(self._pagenum-10, self._pagenum+1)
-
else:
-
if self._page > 4:
-
show_start = self._page - 4
-
else:
-
show_start = 1
-
self.disposeUrl(show_start, show_start + 10)
-
-
#加上最后一页 以及下一页 标签
-
if self._page <> self._pagenum:
-
#下一页
-
self._params["page"] = self._page + 1
-
params_str = urllib.urlencode(self._params)
-
self.pagebar += '+self._url+'?'+params_str+'">' + u"下一页" + ''
-
-
#末页
-
self._params["page"] = self._pagenum
-
params_str = urllib.urlencode(self._params)
-
self.pagebar += '+self._url+'?'+params_str+'">' + u"末页" + ''
-
else:
-
self.pagebar += ''+u"下一页"+''
-
self.pagebar += ''+u"末页"+''
-
-
self._pagebar_html = self._pagebar_html % (self._count, self._page, self._pagenum, self.pagebar)
-
return self._pagenum, self._pagebar_html
-
-
-
#处理分页链接以及显示
-
def disposeUrl(self, start, end):
-
for i in range(start, end):
-
if i == self._page:
-
self.pagebar += ''+str(i)+''
-
else:
-
self._params["page"] = i
-
params_str = urllib.urlencode(self._params)
-
self.pagebar += '+self._url+'?'+params_str+'">' + str(i) + ''
-
-
-
#用于渲染显示页数及样式 (这个是旧的方法已经废弃,可以用于拓展思路,调用的时候推荐用上面的showPage方法)
-
def showPage2(self):
-
pagebar = ""
-
if self._pagenum <> 1:
-
for i in range(1, self._pagenum+1):
-
if i == self._page:
-
pagebar += ''+str(i)+''
-
else:
-
if self._page - i >=4 and i <> 1:
-
i = self._page - 3
-
pagebar += '...'
-
else:
-
if i >= self._page + 6 and i <> self._pagenum:
-
pagebar += '...'
-
i = self._pagenum
-
-
self._params["page"] = i
-
params_str = urllib.urlencode(self._params)
-
pagebar += '+self._url+'?'+params_str+'">' + str(i) + ''
-
else:
-
pagebar += '1'
-
-
self._pagebar_html = self._pagebar_html % (self._count, self._page, self._pagenum, pagebar)
-
return self._pagenum, self._pagebar_html
调用方式如下,发下代码片段。
-
from django.shortcuts import render_to_response #模板用
-
from django.template import RequestContext
-
from django.http import HttpResponse #用于返回原始网页或者JS
-
from django.http import HttpResponseRedirect
-
from django.conf import settings
-
-
from Core_Lib_Page import Core_Lib_Page
-
-
import redis
-
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
-
r = redis.Redis(connection_pool=pool)
-
-
def date_select(request):
-
"""
-
每日查询
-
"""
-
#参数过滤接收
-
params = request.REQUEST.copy()
-
server = params["server"].strip()
-
day = params["day"].strip()
-
-
if not day:
-
fm2 = '%Y-%m-%d'
-
day = time.strftime("%Y-%m-%d", time.localtime(time.time()))
-
-
try:
-
page = int(request.REQUEST.copy()["page"])
-
except KeyError:
-
page = 1
-
except ValueError:
-
page = 1
-
-
#server不为空,具体服查询
-
perpage = 20
-
start = perpage * (page - 1)
-
-
#数据切片尾部
-
end = start + perpage - 1
-
-
if server:
-
count = r.llen("recharge:server:%s:%s" % (server, day))
-
orders_keys = r.lrange("recharge:server:%s:%s" % (server, day), start, end)
-
else:
-
#全服查询
-
#数据总条
-
count = r.llen("recharge:day:oids:%s" % day)
-
-
#订单keys内容出炉
-
orders_keys = r.lrange("recharge:day:oids:%s" % day, start, end)
-
-
#全平台单日充值订单列表
-
orders = []
-
for order_key in orders_keys:
-
order = r.hgetall("recharge:%s" % order_key)
-
order["id"] = order_key
-
orders.append(order)
-
-
urlparams = {"server":server, "day":day}
-
pagenum, pagebar_html = Core_Lib_Page(request.path, page, count, perpage, urlparams).showPage()
-
return render_to_response("admin/into_date_select.html",
-
{
-
"orders_list" : orders,
-
"pagenum" : pagenum,
-
"count" : count,
-
"page" : page,
-
"pagebar_html": pagebar_html,
-
"servers" : get_select_servers(),
-
}
-
)
urlparams 是为了收集参数,然后再传入接下来的分页url中。
数据库我用的是redis.如果用关系型数据库如mysql。也是一样的。
模板我就不发代码了。 可以从这里下载封装类文件:Core_Lib_Page.zip
看下效果。
阅读(1713) | 评论(0) | 转发(0) |