1. 程式人生 > >django 分頁函數

django 分頁函數

-c list paginator obj gen result clas ora ==

實現類似:

上一頁 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

  

django 分頁函數