django 分页函数

实现类似:

上一页 1 ... 4 5 6 7 8 ... 89 下一页

的效果

 

def pageGenerate(fullList,pagenum,urltype,type,currpage):
    pageObj=Paginator(fullList,pagenum)
    totalPage=pageObj.num_pages
    pageitems=pageObj.page(currpage).object_list
    #上下页标签
    if pageObj.page(currpage).has_next():
        next_page=currpage+1
    else:
        next_page=0
    if pageObj.page(currpage).has_previous():
        previous_page=currpage-1
    else:
        previous_page=0

    #数字页,前后各2页
    pageresult=[]
    link={-2:currpage-2,-1:currpage-1,0:currpage,1:totalPage-currpage-1,2:totalPage-currpage-2}
    for k,v in link.items():
        if v>0:
            pageresult.append(currpage+k)

    if pageresult[0]==2:
        pageresult=[1]+pageresult
    elif pageresult[0]>2:
        pageresult=[1,0]+pageresult
    if pageresult[len(pageresult)-1]+1==totalPage:
        pageresult=pageresult+[totalPage]
    elif pageresult[len(pageresult)-1]+1<totalPage:
        pageresult = pageresult + [0,totalPage]

    pagerelease={'pageitems':pageitems,'urltype':urltype,'type':type,'currpage':currpage,'previous_page':previous_page,'pageresult':pageresult,'next_page':next_page}
    return pagerelease

  

posted @ 2018-10-21 11:02  CrossPython  阅读(305)  评论(0编辑  收藏  举报