django 分页

--自定义分页组件

一,自己原创

class PageInfo(object):

def __init__(self,current_page,all_count,per_page,base_url,show_page=11):

## 当前页码,总共多少条数,每页显示多少条数

#all_page  总共页码

try:

self.current_page = int(current_page)

except Exception as e:

self.current_page =1

self.per_page = per_page

print(all_count)

a,b = divmod(all_count,per_page)

if b:

a = a + 1

self.all_page = a

self.show_page =show_page

self.base_url = base_url

def start(self):

return (self.current_page-1) * self.per_page

def end(self):

return self.current_page * self.per_page

def page(self):

# v = "1"

# return  v

page_list = []

haft = int((self.show_page - 1)/2)

print(haft)

if self.current_page > self.all_page:

begin = 1

stop = 2+haft

elif self.current_page > self.all_page-haft:

begin = self.current_page - haft

stop = self.all_page + 1

elif self.current_page >= haft+1:

begin = self.current_page - haft

stop = self.current_page + haft + 1

else :

begin = 1

stop = self.current_page + haft + 1

# if self.current_page < haft+1:

#    begin =1

# else:

#    begin = self.current_page-haft

# if self.current_page > self.all_page-haft:

#    stop = self.all_page+1

#

# else:

#    stop = self.current_page+haft+1

if self.current_page <=1:

prev = "上一页"

else:

prev = "上一页" % (self.base_url,self.current_page-1,)

page_list.append(prev)

if self.current_page >=self.all_page:

after = "下一页"

else:

after = "下一页" % (self.base_url, self.current_page +1,)

for i in range(begin,stop):

if i == self.current_page:

temp="%s" %(self.base_url,i,i,)

else:

temp = "%s" % (self.base_url,i, i,)

page_list.append(temp)

page_list.append(after)

return ''.join(page_list)

def custom(request):

#表示用户当前想要访问的页码:8

all_count = models.UserInfo.objects.all().count()

page_info = PageInfo(request.GET.get('page'),all_count,20,'/app01/custom/')

user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]

return render(request,'custom.html',{'user_list':user_list,'page_info':page_info})

    #######################################################################################

    {% for row in user_list %}

  • {{ row.nid }}  ======  {{ row.username }} 
  • {% endfor %}

{{ page_info.page|safe }}

############################################################################

二,引用bootstarp组件

class PageInfo(object):

def __init__(self,current_page,all_count,per_page,base_url,show_page=11):

## 当前页码,总共多少条数,每页显示多少条数

#all_page  总共页码

try:

self.current_page = int(current_page)

except Exception as e:

self.current_page =1

self.per_page = per_page

print(all_count)

a,b = divmod(all_count,per_page)

if b:

a = a + 1

self.all_page = a

self.show_page =show_page

self.base_url = base_url

def start(self):

return (self.current_page-1) * self.per_page

def end(self):

return self.current_page * self.per_page

def page(self):

# v = "1"

# return  v

page_list = []

haft = int((self.show_page - 1)/2)

print(haft)

if self.current_page > self.all_page:

begin = 1

stop = 2+haft

elif self.current_page > self.all_page-haft:

begin = self.current_page - haft

stop = self.all_page + 1

elif self.current_page >= haft+1:

begin = self.current_page - haft

stop = self.current_page + haft + 1

else :

begin = 1

stop = self.current_page + haft + 1

# if self.current_page < haft+1:

#    begin =1

# else:

#    begin = self.current_page-haft

# if self.current_page > self.all_page-haft:

#    stop = self.all_page+1

#

# else:

#    stop = self.current_page+haft+1

if self.current_page <=1:

prev = "

  • 上一页
  • "

    else:

    prev = "

  • 上一页
  • " % (self.base_url,self.current_page-1,)

    page_list.append(prev)

    if self.current_page >=self.all_page:

    after = "

  • 下一页
  • "

    else:

    after = "

  • 下一页
  • " % (self.base_url, self.current_page +1,)

    for i in range(begin,stop):

    if i == self.current_page:

    temp="

  • %s
  • " %(self.base_url,i,i,)

    else:

    temp = "

  • %s
  • " % (self.base_url,i, i,)

    page_list.append(temp)

    page_list.append(after)

    return ''.join(page_list)

    def custom(request):

    #表示用户当前想要访问的页码:8

    all_count = models.UserInfo.objects.all().count()

    page_info = PageInfo(request.GET.get('page'),all_count,20,'/app01/custom/')

    user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]

    return render(request,'custom.html',{'user_list':user_list,'page_info':page_info})

    ####################################################

    Title

      {% for row in user_list %}

    • {{ row.nid }}  ======  {{ row.username }} 
    • {% endfor %}

    你可能感兴趣的:(django 分页)