zoukankan      html  css  js  c++  java
  • 分页器

     1 from django.shortcuts import render, HttpResponse
     2 
     3 # Create your views here.
     4 from django.core.paginator import Paginator, EmptyPage
     5 from app01 import models
     6 
     7 
     8 # def index(request):
     9 #     # 你们 写:
    10 #     # for i in range(100):
    11 #     #     models.Book.objects.create(name='图书%s'%i,price=10+i)
    12 #     # 我写和以后你们写(批量插入)
    13 #     # 先造成100本书,放到列表中
    14 #     # ll=[]
    15 #     # for i in range(100):
    16 #     #     ll.append(models.Book(name='图书%s'%i,price=10+i))
    17 #     # # 批量插入,两个参数,第一个是对象列表,第二个是一次插入的数据量,不填,默认一次全插入
    18 #     # models.Book.objects.bulk_create(ll)
    19 #
    20 #     # 查询所有图书
    21 #     book_list=models.Book.objects.all()
    22 #     # 分页器--类
    23 #     # 实例化产生一个对象
    24 #     # 两个参数:object_list:对象列表, per_page:每页显示的条数
    25 #     paginator=Paginator(book_list,10)
    26 #     # 对象内的属性
    27 #     # 数据总条数100条
    28 #     # print(paginator.count)
    29 #     # # 总页数  10页
    30 #     # print(paginator.num_pages)
    31 #     # # 页码数的列表
    32 #     # print(paginator.page_range)
    33 #     # # 取到第 x 页 ,返回一个Page对象
    34 #     # current_page=paginator.page(5)
    35 #     # # 当前页码内所有的数据
    36 #     # print(current_page.object_list)
    37 #     # # 是否有下一页
    38 #     # print(current_page.has_next())
    39 #     # # 是否有上一页
    40 #     # print(current_page.has_previous())
    41 #     # # 下一页页码数
    42 #     # print(current_page.next_page_number())
    43 #     # # 上一页的页码数
    44 #     # print(current_page.previous_page_number())
    45 #
    46 #
    47 #     return render(request,'index.html',locals())
    48 
    49 # def index(request):
    50 #     book_list = models.Book.objects.all()
    51 #
    52 #     paginator = Paginator(book_list, 3)
    53 #     # 当前页码
    54 #     try:
    55 #         current_page_num = int(request.GET.get('page'))
    56 #         current_page = paginator.page(current_page_num)
    57 #         # 当前页码所有数据
    58 #         print(current_page.object_list)
    59 #         # 即可以循环current_page.object_list,又可以循环当前页的对象
    60 #         # for item in current_page:
    61 #         #     print(item.name)
    62 #     # except EmptyPage as e:
    63 #     #     # 捕获异常后,跳到第一页
    64 #     #     current_page_num = 1
    65 #     #     current_page = paginator.page(current_page_num)
    66 #     except Exception as e:
    67 #         current_page_num = 1
    68 #         current_page = paginator.page(current_page_num)
    69 #
    70 #     return render(request, 'index.html', locals())
    71 
    72 def index(request):
    73     book_list = models.Book.objects.all()
    74     paginator = Paginator(book_list, 3)
    75     # 如果页码数多,让它显示前5,后5,中间是当前在的页码
    76     try:
    77 
    78         current_page_num = int(request.GET.get('page'))
    79         current_page = paginator.page(current_page_num)
    80         print(current_page.object_list)
    81         # 总页码数,大于11的时候
    82         if paginator.num_pages >11:
    83             # 当前页码数-5大于1的时候,page_range应该是?
    84             if current_page_num-5<1:
    85                 page_range=range(1,12)
    86             elif current_page_num+5>paginator.num_pages:
    87                 #     当前页码数+5大于总页码数,总页码数往前推11个
    88                 page_range=range(paginator.num_pages-10,paginator.num_pages+1)
    89             else:
    90                 page_range = range(current_page_num - 5, current_page_num + 6)
    91         else:
    92             #小于11,有多少页,就显示多少页
    93             page_range=paginator.page_range
    94     except Exception as e:
    95         current_page_num = 1
    96         current_page = paginator.page(current_page_num)
    97 
    98     return render(request, 'index_next.html', locals())
    后台
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
     6     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
     7           integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     8     <title>Title</title>
     9 </head>
    10 <body>
    11 <div class="row">
    12     <div class="col-md-6 col-md-offset-3">
    13 
    14         <table class="table table-striped">
    15             <thead>
    16             <tr>
    17                 <th>书名</th>
    18                 <th>价格</th>
    19             </tr>
    20             </thead>
    21             <tbody>
    22             {% for book in current_page %}
    23                 <tr>
    24                     <td>{{ book.name }}</td>
    25                     <td>{{ book.price }}</td>
    26                 </tr>
    27             {% endfor %}
    28 
    29             </tbody>
    30         </table>
    31         <nav aria-label="Page navigation">
    32             <ul class="pagination">
    33                 {% if current_page.has_previous %}
    34                     <li>
    35                         {#                    <a href="/index/?page={{ current_page_num|add:-1 }}" aria-label="Previous">#}
    36                         <a href="/index/?page={{ current_page.previous_page_number }}" aria-label="Previous">
    37                             <span aria-hidden="true">上一页</span>
    38                         </a>
    39                     </li>
    40                 {% else %}
    41                     <li class="disabled">
    42                         <a href="" aria-label="Previous">
    43                             <span aria-hidden="true">上一页</span>
    44                         </a>
    45                     </li>
    46                 {% endif %}
    47 
    48                 {% for foo in page_range %}
    49                     {% if current_page_num == foo %}
    50                         {# 当前页码等于循环到的页码数,变色#}
    51                         <li class="active"><a href="/index/?page={{ foo }}">{{ foo }}</a></li>
    52                     {% else %}
    53                         <li><a href="?page={{ foo }}">{{ foo }}</a></li>
    54                     {% endif %}
    55 
    56                 {% endfor %}
    57 
    58 
    59 
    60                 {% if current_page.has_next %}
    61                     <li>
    62                         {#                    <a href="/index/?page={{ current_page_num|add:1 }}" aria-label="Next">#}
    63                         <a href="/index/?page={{ current_page.next_page_number }}" aria-label="Next">
    64                             <span aria-hidden="true">下一页</span>
    65                         </a>
    66                     </li>
    67                 {% else %}
    68                     <li class="disabled">
    69                         <a href="" aria-label="Next">
    70                             <span aria-hidden="true">下一页</span>
    71                         </a>
    72                     </li>
    73                 {% endif %}
    74             </ul>
    75         </nav>
    76     </div>
    77 
    78 </div>
    79 </body>
    80 </html>
    前台
  • 相关阅读:
    Web开发者需具备的8个好习惯
    全球十大搜索引擎排名
    ContextWrapper
    PackageManager
    重写equals方法的注意事项
    StackOverflow 并不只是一个问答网站
    android 中ids.xml资源的使用
    The method dismissDialog(int) from the type Activity is deprecated
    百度员工离职总结:如何做个好员工?
    Mysql 自动化任务
  • 原文地址:https://www.cnblogs.com/xuqidong/p/12089094.html
Copyright © 2011-2022 走看看