微商城网站制作,亚当学院网站建设视频教程,北京seo优化厂家,wordpress php 模板修改接着上期代码框架#xff0c;6个主要功能基本实现#xff0c;剩下的就是细节点的完善优化了。
接着优化查询列表分页显示功能#xff0c;有很多菜单功能都有查询列表显示页面情况#xff0c;如果数据量多#xff0c;不分页显示的话#xff0c;页面展示效果就不太好。
本…接着上期代码框架6个主要功能基本实现剩下的就是细节点的完善优化了。
接着优化查询列表分页显示功能有很多菜单功能都有查询列表显示页面情况如果数据量多不分页显示的话页面展示效果就不太好。
本次增加查询列表分页显示功能对一个查询列表功能进行分页改造其他依此类推即可。 第一步Django的分页器paginator简介
Django的分页器paginator是一个内置的分页组件它可以方便地实现分页功能。当页面需要显示大量数据时例如超过10000条使用分页器可以提高阅读体验并减轻服务器压力。
要使用Django的分页器首先需要从django.core.paginator模块中引入Paginator类以及相关的异常模块PageNotAnInteger和EmptyPage。
Paginator是用于管理整个分页的逻辑如控制总共有多少页、页码区间等。而Page类则是用来管理当前这个页面的一些属性。
以下是创建Paginator对象的简单语法 python class Paginator(object_list, per_page, orphans0, allow_empty_first_pageTrue) 其中object_list是你要分页的数据列表per_page是每页显示的数据条数。
例如: 给 Paginator 一个对象列表以及你希望在每个页面上拥有的项目数它提供了访问每页项目的方法 from django.core.paginator import Paginatorobjects [john, paul, george, ringo]p Paginator(objects, 2) p.count
4p.num_pages
2type(p.page_range)
class range_iteratorp.page_range
range(1, 3) page1 p.page(1)page1
Page 1 of 2page1.object_list
[john, paul] page2 p.page(2)page2.object_list
[george, ringo]page2.has_next()
Falsepage2.has_previous()
Truepage2.has_other_pages()
Truepage2.next_page_number()
Traceback (most recent call last):
...
EmptyPage: That page contains no resultspage2.previous_page_number()
1page2.start_index() # The 1-based index of the first item on this page
3page2.end_index() # The 1-based index of the last item on this page
4 p.page(0)
Traceback (most recent call last):
...
EmptyPage: That page number is less than 1p.page(3)
Traceback (most recent call last):
...
EmptyPage: That page contains no results
第二步修改视图文件 ./mysite/study_system/views.py
def getStudyPointsList(request):方法名称: 获取积分明细列表作 者: PandaCode辉weixin公众号: PandaCode辉创建时间: 2023-10-10# 响应容器rsp_dict {}# 获取当前用户名username request.session.get(username)# 根据用户名获取用户对象cur_user StudyUser.objects.get(usernameusername)print(根据用户名查询用户对象: str(cur_user))# 2. 获取要分页的数据集合例如从数据库查询当前用户的全部积分明细 .order_by(-created_time) 降序排列data_list StudyPoint.objects.filter(user_idcur_user).order_by(-created_time)# 3. 每页显示的数据数量items_per_page 5# 4. 创建 Paginator 对象paginator Paginator(data_list, items_per_page)# 5. 获取当前页数从请求参数中获取或者默认为第一页current_page_num request.GET.get(page, 1)1.整个数据表paginator.count 数据总数paginator.num_pages 总页数paginator.page_range 页码的列表2.当前页curuent_page.has_next() 是否有下一页curuent_page.next_page_number() 下一页的页码curuent_page.has_previous() 是否有上一页curuent_page.previous_page_number() 上一页的页码# 6. 获取当前页的数据对象try:current_page_data paginator.page(current_page_num)except EmptyPage:# 处理页码超出范围的情况current_page_data paginator.page(paginator.num_pages)# 获取整个表的总页数total_page paginator.num_pagespag_range []if total_page 11: # 判断当前页是否小于11个pag_range paginator.page_rangeelif total_page 11:if current_page_num 6:pag_range range(1, 11)elif current_page_num paginator.num_pages - 5:pag_range range(total_page - 9, total_page 1)else:pag_range range(current_page_num - 5, current_page_num 5) # 当前页5大于最大页数时# 7. 在模板中使用 current_data_page 来渲染分页数据# 查询待完成任务列表rsp_dict[data_list] data_listrsp_dict[paginator] paginatorrsp_dict[current_page_num] current_page_numrsp_dict[current_page_data] current_page_datarsp_dict[pag_range] pag_rangecontext_object_name study_points_listtemplate_name study_system/home.html# html_file: xxx.html 动态指定模板页面 ; menuTo: task 任务管理 rsp_dict[html_file] study_system/item/studyPointsList.htmlrsp_dict[context_object_name] context_object_namereturn render(request, template_name, rsp_dict)
第三步修改页面模板代码
1. 积分流水列表页面 ./mysite/study_system/templates/study_system/item/studyPointsList.html
style typetext/csstable tr td {font-size: 1.5em;}
/style
!-- 结果显示区 --
div aligncentertable stylewidth: 100%;trtd colspan6 aligncenter积分明细流水/td/trtr stylefont-weight: bold; background: #FFEC8B;text-align: centertd序号/tdtd积分说明/tdtd交易类型/tdtd积分数/tdtd交易时间/tdtd用户名/td/tr{% if current_page_data %}{% for studyPoints in current_page_data %}{% if studyPoints.point_type 0 %}tr stylecolor: blue;text-align: center{# forloop.counter 可以记录循环的次数作为列表序号#}td{{ forloop.counter }}/tdtd{{ studyPoints.point_name }}/tdtd兑换物品/tdtd{{ studyPoints.points_nums }}/tdtd{{ studyPoints.created_time|date:Y-m-d H:i:s }}/tdtd{{ studyPoints.user_id.username }}/td/tr{% elif studyPoints.point_type 1 %}tr stylecolor: red;text-align: centertd{{ forloop.counter }}/tdtd{{ studyPoints.point_name }}/tdtd成功奖励/tdtd{{ studyPoints.points_nums }}/tdtd{{ studyPoints.created_time|date:Y-m-d H:i:s }}/tdtd{{ studyPoints.user_id.username }}/td/tr{% elif studyPoints.point_type 2 %}tr stylecolor: green;text-align: centertd{{ forloop.counter }}/tdtd{{ studyPoints.point_name }}/tdtd失败处罚/tdtd{{ studyPoints.points_nums }}/tdtd{{ studyPoints.created_time|date:Y-m-d H:i:s }}/tdtd{{ studyPoints.user_id.username }}/td/tr{% endif %}{% endfor %}{% else %}trtd colspan6 idcon_title查无记录/td/tr{% endif %}/table
/div
div aligncenter{% include study_system/common/page.html %}
/div 2. 公共页码页面 ./mysite/study_system/templates/study_system/common/page.html
divnav aria-labelPage navigationul classpagination{% if not current_page_data.has_previous %}!--判断是否有上一页--li classdisablea href# aria-labelPreviousspan aria-hiddentrue上一页/span/a/li{% else %}lia href?page{{ current_page_data.previous_page_number }} aria-labelPreviousspan aria-hiddentrue上一页/span/a/li{% endif %}{% for page_range in pag_range %}{% if current_page_num page_range %}!--判断遍历的页数是否为当前页是就添加.avtive 背景色变蓝--li classactivea href?page{{ page_range }}{{ page_range }}/a/li{% else %}lia href?page{{ page_range }}{{ page_range }}/a/li{% endif %}{% endfor %}{% if not current_page_data.has_next %}!-- 判断是否最后一页 --li classdisablea href?page{{ current_page_num }} aria-labelNextspan aria-hiddentrue下一页/span/a/li{% else %}lia href?page{{ current_page_data.next_page_number }} aria-labelNextspan aria-hiddentrue下一页/span/a/li{% endif %}/ul/nav
/div
第四步运行测试
1. 点击查看积分流水列表页面 -------------------------------------------------end -------------------------------------------------