• django【原生分页】


     1 1.urls.py
     2 url(r'^page2/',views.page2),
     3 2.views.py
     4 from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
     5 def page2(request):
     6     customer_list=models.UserType.objects.all()
     7     paginator=Paginator(customer_list,10)
     8     page = request.GET.get('p')
     9     try:
    10         customer_objs=paginator.page(page)
    11     except PageNotAnInteger:
    12         customer_objs=paginator.page(1)
    13     except EmptyPage:
    14         customer_objs=paginator.page(paginator.num_pages)
    15     return render(request,'page2.html',{'customer_list':customer_objs})
    16 3.page2.html
    17 {% load custom_tags %}
    18    <style>
    19         .active{
    20             background: gold;
    21         }
    22     </style>
    23  <table>
    24     <thead>
    25     <tr>
    26         <th>ID</th>
    27         <th>name</th>
    28     </tr>
    29     </thead>
    30     <tbody>
    31     {% for item in customer_list %}
    32         <tr>
    33             <td>{{ item.id }}</td>
    34             <td>{{ item.caption }}</td>
    35         </tr>
    36     {% endfor %}
    37     </tbody>
    38     </table>
    39 <br>
    40     <div class="pagination">
    41     <nav>
    42                 {% if  customer_list.has_previous %}
    43                     <span class="#"><a href="?p={{ customer_list.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">«</span></a></span>
    44                 {% endif %}
    45                 {% for page_num in customer_list.paginator.page_range  %}
    46                     {% guess_page customer_list.number page_num %}
    47                 {% endfor %}
    48                 {% if customer_list.has_next %}
    49                     <span class="#"><a href="?p={{ customer_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">»</span></a></span>
    50                 {% endif %}
    51     </nav>
    52     </div>
    53 4.自定义模板函数
    54    app01/templatetags/custom_tags.py
    55 from django import template
    56 from django.utils.html import format_html
    57 register=template.Library()
    58 @register.filter
    59 def _lower(val):
    60     print('-----val',val)
    61     return val.lower()
    62 @register.simple_tag
    63 def guess_page(current_page,loop_num):
    64         offset=abs(current_page-loop_num)
    65         if offset < 3:
    66             if current_page == loop_num:
    67                 page_ele='<span class="active"><a href="?p=%s">%s</a></span>'%(loop_num,loop_num)
    68             else:
    69                 page_ele='<span class=""><a href="?p=%s">%s</a></span>'%(loop_num,loop_num)
    70             return format_html(page_ele)
    71         else:
    72             return ""
  • 相关阅读:
    多态与多态性,鸭子类型
    类的继承与派生,抽象类
    常用模块
    模块与包
    三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
    函数装饰器
    函数基础
    文件处理
    数据类型
    Spring源码分析--IOC流程
  • 原文地址:https://www.cnblogs.com/weibiao/p/6273669.html
Copyright © 2020-2023  润新知