django 自定义html5,Django自定义分页功能的实现

def query(request):

"""

current_page:当前页码,默认第一页

line_num:每页显示的行数,默认10行

table_line_count:总行数

page_count:总页数

display_page_num_count:显示的页数码标签总个数,默认10个

"""

current_page = int(request.GET.get('p',1))

line_num = int(request.COOKIES.get("per_page_line_num",10))

table_line_count=tablename.objects.count()            ####统计表总行数,tablename指数据库中的表,根据实际修改

display_page_num_count=10

####求总页数

a,b=divmod(table_line_count,line_num)      ####求除数和余数,例如divmod(5,1)=(1,1)

if b>0:

page_count=a+1

else:

page_count = a

"""

first_page:首页

last_page:尾页

prev_page:上一页

next_page:下一页

first_display_page_num:第一个显示的页码

last_display_page_num:最后一个显示的页码

"""

first_page = "%s" % (1, "首页")

last_page = "%s" % (page_count, "尾页")

if current_page-1==0:

prev_page="%s" % (current_page, "上一页")

else:

prev_page="%s" % (current_page-1, "上一页")

if current_page==page_count:

next_page="%s" % (current_page, "下一页")

else:

next_page="%s" % (current_page+1, "下一页")

page_str=""

page_str+=first_page

page_str+=prev_page

if page_count<=display_page_num_count:

first_display_page_num = 1

last_display_page_num = page_count + 1

else:

# 总页数大于10页(只显示10个页数标签)

#    如果当前页数《=5

#        显示1-10页数标签

#    如果当前页数x》5

#        显示 x-4到x+5页数标签

#    如果当前页数x》总页数-5

#        显示 总页数-9 到总页数之间的标签

if current_page_num<=5:

first_display_page_num=1

last_display_page_num=display_page_num_count

elif 5

first_display_page_num=current_page_num-5+1

last_display_page_num=current_page_num+5

else:

first_display_page_num=page_count-5-5+1

last_display_page_num=page_count

for i in range(first_display_page_num, last_display_page_num):

if i == current_page:

page_str += "%s" % (i, i)

else:

page_str += "%s" % (i, i)

page_str += next_page

page_str += last_page

page_str = mark_safe(page_str)

response = render(request, 'query.html', locals())

response.set_cookie("per_page_line_num",line_num,path='/query/')  ####设置每页显示的行数

return response

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值