首页 > 代码库 > django学习2分页

django学习2分页

  在django中有一个分页的函数,但是我个人觉得不是很好,有的功能不能定制感觉有点不方便,在另外一方面出于学习的目的,我又自己写了一遍分页的代码用户来实现定制化的内容,在django中自带的分页函数是这样的(django-pagination)使用的时候只需要调用一下就可以了。

  现在我们自己来实现一下分页的功能,这个是最后调用的格式

技术分享
from django.shortcuts import  render_to_response
from DjangoBlog.blog import models
from DjangoBlog.blog.common import  try_int
from DjangoBlog.blog import html_helper

def index(request,page):
    page  = try_int(page ,1)
    count = models.Host.objects.all().count()

    pageObj = html_helper.PageInfo(page, count)
    result = models.Host.objects.all()[pageObj.start : pageObj.end]
    page_string = html_helper.Pager(page,pageObj.all_page_count())

    ret = {data:result ,count: count,page:page_string}
    return render_to_response(index.html,ret)
View Code

  首先我们要从用户的url中获取到page的数据然后穿到后台经行处理,就比如这个博客园的url:https://www.cnblogs.com/#p6 我们把 6 获取到这里我们要判断一下用户传过来的是不是实数类型,免得当有用户非法输入时,后台处理出错误,这里我们编写一个common.py的工具类,等以后要使用的时候,不用做重复的劳动。

技术分享
def try_int(arg, default):
    ret = None
    try:
        arg = int(arg)
    except Exception:
        arg = default
    return arg
View Code

  通过这个就可以把错误的输入拦截下来了。ps其实用一下正则化也可以实现相关的功能,这里我们就先这样写 >.<

  这个就是实现分页功能的所有函数了,这里我先贴出来然后一个一个来解释把!

技术分享
from django.utils.safestring  import mark_safe
class PageInfo():
    ‘‘‘
    per_item = 5
    start = (page - 1)* per_item
    end = page * per_item
    temp = divmod(count,per_item)
    if temp[1] == 0:
        all_page_count = temp[0]
    else:
        all_page_count = temp[0] + 1
        ‘‘‘
    def __init__(self, current_page, all_count,per_item=5):
        self.CurrentPage = current_page
        self.AllCount = all_count
        self.PerItem = per_item
    @property
    def start(self):
        return (self.CurrentPage-1)*self.PerItem
    @property
    def end(self):
        return self.CurrentPage*self.PerItem

    @property
    def all_page_count(self):
        temp = divmod(self.AllCount, self.PerItem)
        if temp[1] == 0:
            all_page_count = temp[0]
        else:
            all_page_count = temp[0] + 1
        return all_page_count


def Pager(page, all_page_count):
    ‘‘‘page :当前页
        all_page_count:总页数‘‘‘
    page_html = []
    first_page = "<a href = http://www.mamicode.com/‘/index/1‘>首页"
    page_html.append(first_page)

    if page <= 1:
        prv_html = "<a  href = http://www.mamicode.com/‘/index/%d‘>上一页" % (1,)
    else:
        prv_html = "<a  href = http://www.mamicode.com/‘/index/%d‘>上一页" % (page - 1,)
        page_html.append(prv_html)

    begin = page - 6
    end = page + 5
    if all_page_count < 12:
        begin = 0
        end = all_page_count
    else:
        if page < 6:
            begin = 0
            end = 12
        else:
            if page + 6 > all_page_count:
                begin = page - 6
                end = all_page_count
            else:
                begin = page - 6
                end = page + 5
    for i in range(begin,end):
        if page == i + 1:
            a_html = "<a class = ‘selected‘‘ href = http://www.mamicode.com/‘/index/%d‘>%d" % (i + 1, i + 1)
        else:
            a_html = "<a  href = http://www.mamicode.com/‘/index/%d‘>%d" % (i + 1, i + 1)
        page_html.append(a_html)
    if page >= all_page_count:
        next_html = "<a  href = http://www.mamicode.com/‘/index/#‘>下一页"
    else:
        next_html = "<a  href = http://www.mamicode.com/‘/index/%d‘>下一页" % (page + 1,)
        page_html.append(next_html)

    end_page = "<a href = http://www.mamicode.com/‘/index/%d‘>尾页" % (all_page_count,)
    page_html.append(end_page)

    page_string = mark_safe(‘‘.join(page_html))

    return  page_string
View Code

首先我们需要确定一下起始位置和终止位置再用divmod函数确定一下最终有多少页,然后再根据一般的分页操作就可以完成了

django学习2分页