Django第七天 自己写分页

  def pager(self):
        #v='<a href=/app01/customs.html?page=19>19</a>'
        #以字符串形式传入前端   不过需要加|safe
        half=int((self.show_page-1)/2)
        ##用总的显示页面数量的一半表示前后各有多少个,为了前后数量一致总数为单数。
        if self.all_page<self.show_page:

            begin=1
            stop=self.all_page
            ##如果数据总数小于显示页面数 永远从第一页开始,总页数为最大页数
        else:
            if self.current_page<half:
                begin=1
                stop=self.show_page+1
                ##当前页小于总页数一半
            else:
                if self.current_page+half>self.all_page:
                    begin=self.all_page-self.show_page+1
                    stop=self.all_page+1

                else:
                    begin =self.current_page-half
                    stop = self.current_page+half+1
        # begin=self.current_page-half
        # #起始位置
        # stop=self.current_page+half+1
        # #末尾位置
        page_list=[]
        frist='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=1>首页</a>'
        page_list.append(frist)
        if self.current_page <=1:
            prev = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=#>上一页</a>'
        else:
            prev='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>上一页</a>' % (self.current_page-1)
        page_list.append(prev)
        ##创建一个列表来列出所有的页数
        for i in range(begin,stop):
            ##循环所有的页面
            if i == self.current_page:
                temp = '<a style ="display:inline-block;background-color:red;margin:3px;padding:3px"href=/app01/customs.html?page=%s>%s</a>' % (
                self.current_page, self.current_page)
                ##判断是否为当前页,如果为当前页就显示为底色为红色
            else:
                temp ='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>%s</a>'%(i,i)
            #调整了一下样式
            page_list.append(temp)
        if self.current_page >=self.all_page:
            nex = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=#>下一页</a>'
        else:
            nex = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>下一页</a>' % (
                        self.current_page + 1)
        page_list.append(nex)
        last=prev = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>尾页</a>'%(self.all_page)
        page_list.append(last)
        return ''.join(page_list)
        ##用.join 方法temp字符串拼接起来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值