zoukankan      html  css  js  c++  java
  • 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 ""
  • 相关阅读:
    openpyxl读取Excel数据
    查找xml中的接口名及涉及表名并输出
    sqlalchemy 简介
    linux文件查看
    网页的MVC模式简介
    python 最小二乘拟合,反卷积,卡方检验
    生成随机图片验证码
    图形界面
    requests(第三方模块) 请求、登录、下载网页
    ( 转 ) 什么是 JWT -- JSON WEB TOKEN
  • 原文地址:https://www.cnblogs.com/weibiao/p/6273669.html
Copyright © 2011-2022 走看看