分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

分页

时间:2020-03-12

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

本文章向你们介绍分页,主要包括分页使用实例、应用方法、基本知识点总结和须要注意事项分页,具有一定的参考价值分页,须要的同学可以参考一下。

分页

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

 1 from django.shortcuts import render 2 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 3  4 L = [] 5 for i in range(999): 6     L.append(i) 7  8 def index(request): 9     current_page = request.GET.get('p')10 11     paginator = Paginator(L, 10)12     # per_page: 每页显示条目数量13     # count:    数据总个数14     # num_pages:总页数15     # page_range:总页数的索引范围,如: (1,10),(1,200)16     # page:     page对象17     try:18         posts = paginator.page(current_page)19         # has_next              是否有下一页20         # next_page_number      下一页页码21         # has_previous          是否有上一页22         # previous_page_number  上一页页码23         # object_list           分页之后的数据列表24         # number                当前页25         # paginator             paginator对象26     except PageNotAnInteger:27         posts = paginator.page(1)28     except EmptyPage:29         posts = paginator.page(paginator.num_pages)30     return render(request, 'index.html', {'posts': posts})

外置分页view函数

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

 1  2  3 "en"> 4     "UTF-8"> 5      6  7  8 
    9 {% for item in posts %}10
  • {{ item }}
  • 11 {% endfor %}12
13 14
class="pagination">15 class="step-links">16 {% if posts.has_previous %}17 "?p={{ posts.previous_page_number }}">Previous18 {% endif %}19 class="current">20 Page {{ posts.number }} of {{ posts.paginator.num_pages }}.21 22 {% if posts.has_next %}23 "?p={{ posts.next_page_number }}">Next24 {% endif %}25 26 27
28 29

外置分页HTML页面

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

 1 from django.shortcuts import render 2 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 3  4  5 class CustomPaginator(Paginator): 6     def __init__(self, current_page, max_pager_num, *args, **kwargs): 7         """ 8         :param current_page: 当前页 9         :param max_pager_num:最多显示的页码个数10         :param args:11         :param kwargs:12         :return:13         """14         self.current_page = int(current_page)15         self.max_pager_num = max_pager_num16         super(CustomPaginator, self).__init__(*args, **kwargs)17 18     def page_num_range(self):19         # 当前页面20         # self.current_page21         # 总页数22         # self.num_pages23         # 最多显示的页码个数24         # self.max_pager_num25         print(1)26         if self.num_pages < self.max_pager_num:27             return range(1, self.num_pages + 1)28         print(2)29         part = int(self.max_pager_num / 2)30         if self.current_page - part < 1:31             return range(1, self.max_pager_num + 1)32         print(3)33         if self.current_page + part > self.num_pages:34             return range(self.num_pages + 1 - self.max_pager_num, self.num_pages + 1)35         print(4)36         return range(self.current_page - part, self.current_page + part + 1)37 38 39 L = []40 for i in range(999):41     L.append(i)42 43 def index(request):44     current_page = request.GET.get('p')45     paginator = CustomPaginator(current_page, 11, L, 10)46     # per_page: 每页显示条目数量47     # count:    数据总个数48     # num_pages:总页数49     # page_range:总页数的索引范围,如: (1,10),(1,200)50     # page:     page对象51     try:52         posts = paginator.page(current_page)53         # has_next              是否有下一页54         # next_page_number      下一页页码55         # has_previous          是否有上一页56         # previous_page_number  上一页页码57         # object_list           分页之后的数据列表58         # number                当前页59         # paginator             paginator对象60     except PageNotAnInteger:61         posts = paginator.page(1)62     except EmptyPage:63         posts = paginator.page(paginator.num_pages)64 65     return render(request, 'index.html', {'posts': posts})

扩充外置分页view函数

分页 分页使用实例、应用技巧、基本知识点总结和需要注意事项

 1  2  3 "en"> 4     "UTF-8"> 5      6  7  8  9 
    10 {% for item in posts %}11
  • {{ item }}
  • 12 {% endfor %}13
14 15
class="pagination">16 class="step-links">17 {% if posts.has_previous %}18 "?p={{ posts.previous_page_number }}">Previous19 {% endif %}20 21 {% for i in posts.paginator.page_num_range %}22 "?p={{ i }}">{{ i }}23 {% endfor %}24 25 {% if posts.has_next %}26 "?p={{ posts.next_page_number }}">Next27 {% endif %}28 29 30 class="current">31 Page {{ posts.number }} of {{ posts.paginator.num_pages }}.32 33 34
35 36

扩充外置分页html页面

 1 __author__ = 'Administrator' 2 from django.utils.safestring import mark_safe 3  4  5 class Pagination(object): 6     def __init__(self, current_page, data_count, per_page_count=7, pager_num=7): 7         try: 8             self.current_page = int(current_page) 9         except Exception as e:10             self.current_page = 111         self.data_count = data_count12         self.per_page_count = per_page_count13         self.pager_num = pager_num14 15     @property16     def start(self):17         return (self.current_page - 1) * self.per_page_count18 19     @property20     def end(self):21         return self.current_page * self.per_page_count22 23     @property24     def total_count(self):25         v, y = divmod(self.data_count, self.per_page_count)26         if y:27             v += 128         return v29 30     def page_str(self, base_url):31         page_list = []32 33         if self.total_count < self.pager_num:34             start_index = 135             end_index = self.total_count + 136         else:37             if self.current_page <= (self.pager_num + 1) / 2:38                 start_index = 139                 end_index = self.pager_num + 140             else:41                 start_index = self.current_page - (self.pager_num - 1) / 242                 end_index = self.current_page + (self.pager_num + 1) / 243                 if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:44                     end_index = self.total_count + 145                     start_index = self.total_count - self.pager_num + 146 47         if self.current_page == 1:48             prev = '
  • 上一页
  • '49 else:50 prev = '
  • 上一页
  • ' % (base_url, self.current_page - 1,)51 page_list.append(prev)52 53 for i in range(int(start_index), int(end_index)):54 if i == self.current_page:55 temp = '
  • %s
  • ' % (base_url, i, i)56 else:57 temp = '
  • %s
  • ' % (base_url, i, i)58 page_list.append(temp)59 60 if self.current_page == self.total_count:61 nex = '
  • 下一页
  • '62 else:63 nex = '
  • 下一页
  • ' % (base_url, self.current_page + 1,)64 page_list.append(nex)65 66 # jump = """67 # GO68 # 74 # """ % (base_url,)75 #76 # page_list.append(jump)77 78 page_str = mark_safe("".join(page_list))79 80 return page_str

    自定义分页组件

    收藏文章

    原文链接:https://www.dyfan.vip/22430.html,请用户仔细辨认内容的真实性,避免上当受骗!

    0
    显示验证码
    没有账号?注册  忘记密码?