Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5120211
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2014-03-14 16:15:06

前段时间用django给游戏写管理后台,用到了里面的分页组件。看了一下竟然不支持分片,一股脑取出数据,然后分页。感觉太浪费资源,自己就写了一个支持分片的分页类。

先上代码:



  1. #! /usr/bin/env python
  2. #coding=utf-8

  3. #-----------------------------------
  4. # Name: Core_Lib_Page.py
  5. # Desc: 分页封装类
  6. # Author: Lunay Zhao 2013-12-14
  7. #-----------------------------------

  8. import math
  9. import urllib
  10. class Core_Lib_Page(object):
  11.     _pagebar_html = \
  12.     """
  13.     
    -page">
  14.     -totlerecord">%d
  15.     -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


  • 调用方式如下,发下代码片段。


    1. from django.shortcuts import render_to_response #模板用
    2. from django.template import RequestContext
    3. from django.http import HttpResponse #用于返回原始网页或者JS
    4. from django.http import HttpResponseRedirect
    5. from django.conf import settings

    6. from Core_Lib_Page import Core_Lib_Page

    7. import redis
    8. pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
    9. r = redis.Redis(connection_pool=pool)

    10. def date_select(request):
    11.     """
    12.     每日查询
    13.     """
    14.     #参数过滤接收
    15.     params = request.REQUEST.copy()
    16.     server = params["server"].strip()
    17.     day = params["day"].strip()
    18.     
    19.     if not day:
    20.         fm2 = '%Y-%m-%d'
    21.         day = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    22.         
    23.     try:
    24.         page = int(request.REQUEST.copy()["page"])
    25.     except KeyError:
    26.         page = 1
    27.     except ValueError:
    28.         page = 1
    29.     
    30.     #server不为空,具体服查询
    31.     perpage = 20
    32.     start = perpage * (page - 1)
    33.     
    34.     #数据切片尾部
    35.     end = start + perpage - 1
    36.     
    37.     if server:
    38.         count = r.llen("recharge:server:%s:%s" % (server, day))
    39.         orders_keys = r.lrange("recharge:server:%s:%s" % (server, day), start, end)
    40.     else:
    41.         #全服查询
    42.         #数据总条
    43.         count = r.llen("recharge:day:oids:%s" % day)
    44.         
    45.         #订单keys内容出炉
    46.         orders_keys = r.lrange("recharge:day:oids:%s" % day, start, end)
    47.         
    48.     #全平台单日充值订单列表
    49.     orders = []
    50.     for order_key in orders_keys:
    51.         order = r.hgetall("recharge:%s" % order_key)
    52.         order["id"] = order_key
    53.         orders.append(order)
    54.         
    55.     urlparams = {"server":server, "day":day}
    56.     pagenum, pagebar_html = Core_Lib_Page(request.path, page, count, perpage, urlparams).showPage()
    57.     return render_to_response("admin/into_date_select.html",
    58.         {
    59.             "orders_list" : orders,
    60.             "pagenum" : pagenum,
    61.             "count" : count,
    62.             "page" : page,
    63.             "pagebar_html": pagebar_html,
    64.             "servers" : get_select_servers(),
    65.         }
    66.     )

    urlparams 是为了收集参数,然后再传入接下来的分页url中。

    数据库我用的是redis.如果用关系型数据库如mysql。也是一样的。

    模板我就不发代码了。 可以从这里下载封装类文件:Core_Lib_Page.zip

    看下效果。










    阅读(1715) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~