zoukankan      html  css  js  c++  java
  • Flask 实现分页

    pager.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div class="container">
        <div class="row " style="margin-top: 10px">
                <ul>
                    {% for foo in index_list %}
                        <li>{{ foo }}</li>
                    {% endfor %}
                </ul>
    
                <nav aria-label="Page navigation" class="pull-right">
                    <ul class="pagination">
                        {{ html|safe }}
                    </ul>
                </nav>
        </div>
    </div>
    </body>
    </html>
    

    pager.py

    from urllib.parse import urlencode,quote,unquote
    class Pagination(object):
        def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11):
            try:
                current_page = int(current_page)
            except Exception as e:
                current_page = 1
            if current_page <=0:
                current_page = 1
            self.current_page = current_page
            # 数据总条数
            self.total_count = total_count
            # 每页显示10条数据
            self.per_page_count = per_page_count
            # 页面上应该显示的最大页码
            max_page_num, div = divmod(total_count, per_page_count)
            if div:
                max_page_num += 1
            self.max_page_num = max_page_num
            # 页面上默认显示11个页码(当前页在中间)
            self.max_pager_count = max_pager_count
            self.half_max_pager_count = int((max_pager_count - 1) / 2)
            # URL前缀
            self.base_url = base_url
            # request.GET
            import copy
            params = copy.deepcopy(params)
            # params._mutable = True
            get_dict = params.to_dict()
            # 包含当前列表页面所有的搜/索条件
            # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
            # self.params[page] = 8
            # self.params.urlencode()
            # source=2&status=2&gender=2&consultant=1&page=8
            # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8"
            # href="%s?%s" %(self.base_url,self.params.urlencode())
            self.params = get_dict
        @property
        def start(self):
            return (self.current_page - 1) * self.per_page_count
    
        @property
        def end(self):
            return self.current_page * self.per_page_count
        def page_html(self):
            # 如果总页数 <= 11
            if self.max_page_num <= self.max_pager_count:
                pager_start = 1
                pager_end = self.max_page_num
            # 如果总页数 > 11
            else:
                # 如果当前页 <= 5
                if self.current_page <= self.half_max_pager_count:
                    pager_start = 1
                    pager_end = self.max_pager_count
                else:
                    # 当前页 + 5 > 总页码
                    if (self.current_page + self.half_max_pager_count) > self.max_page_num:
                        pager_end = self.max_page_num
                        pager_start = self.max_page_num - self.max_pager_count + 1   #倒这数11个
                    else:
                        pager_start = self.current_page - self.half_max_pager_count
                        pager_end = self.current_page + self.half_max_pager_count
    
            page_html_list = []
            # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
            # 首页
            self.params['page'] = 1
            first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,urlencode(self.params),)
            page_html_list.append(first_page)
            # 上一页
            self.params["page"] = self.current_page - 1
            if self.params["page"] < 1:
                pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>' % (self.base_url, urlencode(self.params))
            else:
                pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>' % ( self.base_url, urlencode(self.params))
            page_html_list.append(pervious_page)
            # 中间页码
            for i in range(pager_start, pager_end + 1):
                self.params['page'] = i
                if i == self.current_page:
                    temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
                else:
                    temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
                page_html_list.append(temp)
    
            # 下一页
            self.params["page"] = self.current_page + 1
            if self.params["page"] > self.max_page_num:
                self.params["page"] = self.current_page
                next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >' % (self.base_url, urlencode(self.params))
            else:
                next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>' % (self.base_url, urlencode(self.params))
            page_html_list.append(next_page)
            # 尾页
            self.params['page'] = self.max_page_num
            last_page = '<li><a href="%s?%s">尾页</a></li>' % (self.base_url, urlencode(self.params),)
            page_html_list.append(last_page)
    
            return ''.join(page_html_list)
    

    app.py

    from flask import Flask,render_template,request,redirect
    from pager import Pagination
    from urllib.parse import urlencode
    app = Flask(__name__)
    
    @app.route('/pager')
    def pager():
        li = []
        for i in range(1,100):
            li.append(i)
    
        pager_obj = Pagination(request.args.get("page",1),len(li),request.path,request.args,per_page_count=10)
        index_list = li[pager_obj.start:pager_obj.end]
        html = pager_obj.page_html()
        return render_template("pager.html",index_list=index_list, html = html)
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  • 相关阅读:
    【转】浅谈一个网页打开的全过程(涉及DNS、CDN、Nginx负载均衡等)
    【转】1.2 CDN的基本工作过程
    【转】 最新版chrome谷歌浏览器Ajax跨域调试问题
    【转】网段,子网掩码,网络标识,IP划分
    【转】默认网关有什么用?我应当怎么填写默认网关和DNS呢
    【转】DHCP工作过程详解
    【转】WINS服务器与DNS服务器有什么区别?
    46. Permutations 排列数
    30. Substring with Concatenation of All Words
    29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/csnd/p/12058939.html
Copyright © 2011-2022 走看看