zoukankan      html  css  js  c++  java
  • tornado&django --- 分页

    tornado

    1,urls.py

    import config
    import os
    from views.view import IndexHadnler,SearchHadnler,SearchresHadnler
    class Application(tornado.web.Application):
        def __init__(self):
            heandlers = [
                (r'/',IndexHadnler),
                (r'/search',SearchHadnler),
                (r'/search_res/(?P<page>d*)',SearchresHadnler),
                # (r'/pay',PayHadnler)
            ]
    
            super(Application,self).__init__(heandlers,**config.settings,address='0.0.0.0')

    2,实现分页功能及展示

    class Pagination:
        """
        分页
        """
     
        def __init__(self, current_page='1', page_item=1):
            all_page, c = divmod(page_item, 5)
            if c > 0:
                all_page += 1
            try:
                current_page = int(current_page)
            except:
                current_page = 1
            if current_page < 1:
                current_page = 1
     
            self.current_page = current_page  # 当前页
            self.all_page = all_page  # 总页数
     
        @property
        def start(self):
            """
            显示数据的起点索引
            :return:
            """
            return (self.current_page - 1) * 5
     
        @property
        def end(self):
            """
            显示数据的末尾索引
            :return:
            """
            return self.current_page * 5
     
        def page_num_show(self, baseurl):
            """
            写入{% raw str_page %}模板中的内容
            :param baseurl: 该段代码不仅可以在/index/中使用,也可以在/home/等等页码使用,
            :return: 返回一段字符串形式的html代码块,包括首页,页码数,上一页等等内容
            """
            # 计算9个页码的起始索引
            list_page = []
            if self.current_page <= 4:
                s = 0
                e = min(self.all_page, 9)
            elif self.current_page > self.all_page - 4:
                s = max(0, self.all_page - 9)
                e = self.all_page
            else:
                s = self.current_page - 5
                e = self.current_page + 4
            # 首页
            first_page = '<a href="%s1">首页</a>' % (baseurl)
            list_page.append(first_page)
     
            # 上一页current_page-1
            if self.current_page <= 1:
                prev_page = '<a href="javascript:void(0);">上一页</a>'
            else:
                prev_page = '<a href="%s%s">上一页</a>' % (baseurl, self.current_page - 1)
            list_page.append(prev_page)
     
            #9个页码数
            for p in range(s, e):
                if p + 1 == self.current_page:
                    temp = '<a href="%s%s" class="active">%s</a>' % (baseurl, p + 1, p + 1)
                    list_page.append(temp)
                else:
                    temp = '<a href="%s%s">%s</a>' % (baseurl, p + 1, p + 1)
                    list_page.append(temp)
     
            # 下一页next_page+1
            if self.current_page >= self.all_page:
                next_page = '<a href="javascript:void(0);">下一页</a>'
            else:
                next_page = '<a href="%s%s">下一页</a>' % (baseurl, self.current_page + 1)
            list_page.append(next_page)
     
            # 尾页
            last_page = '<a href="%s%s">尾页</a>' % (baseurl, self.all_page)
            list_page.append(last_page)
     
            # # 页面跳转
            # jump = """<input type="text"/><a οnclick="Jump('%s',this);">go</a>""" % (baseurl,)
            # script = """<script>
            #     function Jump(url,self){
            #         var v=self.previousElementSibling.value;
            #         if (v.trim().length>0){
            #             location.href=url+v;
            #         }
            # }
            # </script>"""
            # list_page.append(jump)
            # list_page.append(script)
     
            str_page = "".join(list_page)
            return str_page
    
    
    
    class SearchresHadnler(BaseHandler):
        async def get(self,page):
            page_obj = Pagination(page, len(info_res))
            # 当前页显示的数据
            current_list = info_res[page_obj.start:page_obj.end]
            # 当前页显示的页码数相关html代码
            str_page = page_obj.page_num_show('/search_res/')
            self.render('info.html', info=current_list, current_page=page_obj.current_page, str_page=str_page)

    3,info.html

    <!DOCTYPE html>
    <!-- <html lang="en"> -->
        <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/aui.css" />
        <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="../static/top/gameadmin/libs/aui/css/bootstrap-theme.min.css" /> 
        <link type="text/css" rel="stylesheet"  href="../static/top/gameadmin/css/jquery.dataTables.min.css">
        <link href="../static/top/gameadmin/css/H-ui.min.css" rel="stylesheet" type="text/css" />
        <link href="../static/top/gameadmin/css/H-ui.admin.css" rel="stylesheet" type="text/css" />    
        <script src="../static/top/gameadmin/libs/jquery-1.12.1.min.js" ></script>
        <script src="../static/top/gameadmin/libs/bootstrap.min.js" ></script>
        <script src="../static/top/gameadmin/libs/axios.js" ></script>
        <script src="../static/top/gameadmin/libs/jquery.cookie.js" ></script>
        <script>
            var le = {
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
                    "sInfoEmpty": "显示第 0 至 0 项结果,共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "表中数据为空",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    }
                }
            } 
        </script>
    <head>
        <!-- <meta charset="UTF-8"> -->
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <!-- <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/> -->
        <!-- <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <meta http-equiv="pragma" content="no-cache" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="0" /> -->
        <title>硕果电影</title>
        
        
    
    </head>
    <body>
    
    <!-- <div class="aui-content aui-margin-b-15"> -->
        <section class="container-fluid page-404 minWP text-c" style="height: 20px;">
            <p class="error-title">
                <span class="va-m"> 电影列表</span>
            </p>
        </section>
    
        <div class="container" style="margin-top: 70px">
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                        <table id="dt" style="border:black 3px solid" class="table table-hover">
                            <thead><tr class="warning"><td>影片名称</td><td>影片类别</td><td>更新时间</td><td>播放</td></tr></thead>
                            <tbody>
                            {% for i in info %}
                                <tr>
                                    <td>{{i['title']}}</td>
                                    <td>{{i['typ']}}</td>
                                    <td>{{i['update_time']}}</td>
                                    <td>
                                        {%for j in i['info_url']%}
                                        <a href="{{j['url']}}">{{j['name']}}</a>
                                        {%end%}
                                    </td>
                                </tr>
                            {%end%}
                            </tbody>
                        </table>
                        <div class="page_num">
                            {% raw str_page %}
                        </div>
               
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="../static/top/gameadmin/js/jquery.dataTables.min.js" ></script>
    <script type="text/javascript">
        $(document).ready(function() {
            "bFilter": true, //过滤功能
            
            $("#dt").DataTable(
            // "bFilter": true,//过滤功能
            // 'bLengthChange': false,//改变每页显示数据数量
            le,
            );
            
        })
    </script>
    </body>
    </html>

    效果如下:

    Django

    1,前端

    <nav aria-label="Page navigation">
                            <div class="pagination">
                                <ul class="pagination">
                                {% if books.has_previous %} 
                                    <a class='active' href="?page={{ books.previous_page_number }}">上一页</a>
                                {% endif %}
                    
                                <!-- {% for num in books.paginator.page_range%} 
                                    {%if pindex == books.number%}
                                        <a href="">{{ num }}</a>
                                    {%else%}
                                        <a href="/books/borrow_show/{{num}}">{{ num }}</a>
                                    {%endif%}
                                 {% endfor %} -->
                                <span class="current">
                                    第{{ books.number }}页</span>
    
                                {% if books.has_next %}
                                    <a class='active' href="?page={{ books.next_page_number }}">下一页</a>
                                {% endif %}  共{{ books.paginator.num_pages }}页
                                </ul>
                            </div>
                        </nav>

    2,后端

    from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
    class My_player(View):
        @method_decorator(check_login)
        def get(self,request):
            res = request.COOKIES.get("token")
            ret = Account.objects.filter(uuid=int(res)).first()
            result = Friendscircle.objects.filter(accountid=ret.id).first()
            #玩家信息
            info = Friendscircleaccount.objects.filter(fcircleid=result.id).values('accountid')
       
            id_list = []
            for i in info:
                if i['accountid'] != ret.id:
                    id_list.append(i['accountid'])
     
            book_list = Account.objects.filter(id__in=id_list)
            paginator = Paginator(book_list, 5)
            page = request.GET.get('page')
            try:
                books = paginator.page(page)
            # todo: 注意捕获异常
            except PageNotAnInteger:
                # 如果请求的页数不是整数, 返回第一页。
                books = paginator.page(1)
            except InvalidPage:
                # 如果请求的页数不存在, 重定向页面
                return HttpResponse('找不到页面的内容')
            except EmptyPage:
                # 如果请求的页数不在合法的页数范围内,返回结果的最后一页。
                books = paginator.page(paginator.num_pages)
    
            return render(request,'myPlayer.html', {'books': books})

  • 相关阅读:
    【转】centos(原生yum系通用)安装xfce便捷方法
    [转载]TortoiseGit安装与使用
    [转载]eclipse的远程调试功能配置
    oracle加密-des 简单举例.
    一个使用Servlet文件实现文件下载的实例
    (转载)JWebUnit做Web项目自动化测试
    得到类所在的jar包路径
    java实现https ssl请求url
    Java中用HttpsURLConnection访问Https链接
    [转载][Groovy] Groovy与Java的区别(一)
  • 原文地址:https://www.cnblogs.com/xcsg/p/12132736.html
Copyright © 2011-2022 走看看