6-Django项目--分页模块化封装参数共存

devtools/2024/12/23 0:41:46/

目录

utils/page_data.py

分页模块化封装

python%20package-toc" style="margin-left:80px;">在app当中创建一个python package

在当前包里面创建py文件

参数共存

完整代码


utils/page_data.py

--包里创建py文件.

python"># -*- coding:utf-8 -*-
from django.utils.safestring import mark_safe
from copy import deepcopyclass PageData(object):def __init__(self, request, queryset, page_size=10, plus=2, page_param="page"):"""request: 请求queryset:数据表查询的结果page_size:每页展示多少条数据plus:当前页,前后各展示多少个页码"""get_query_dict = deepcopy(request.GET)self.query_dict = get_query_dictself.page_param = page_param# 分页page = request.GET.get(page_param, "1")  # 当前所在的页面# 判断字符创当中全部都是数字if page.isdecimal():page = int(page)else:page = 1self.page = pageself.start = (page - 1) * page_sizeself.end = page * page_size# 分页操作self.page_queryset = queryset[self.start: self.end]page_count = queryset.count()page_count, div = divmod(page_count, page_size)if div:page_count += 1self.page_count = page_countself.plus = plusdef page_html(self):if self.page_count <= 2 * self.plus + 1:start_page = 1end_page = self.page_countelse:# 当前选中的页数小于等于三if self.page <= self.plus:start_page = 1end_page = 2 * self.plus + 1else:# 当前页 + 2if (self.page + self.plus) > self.page_count:start_page = self.page_count - self.plus * 2end_page = self.page_countelse:start_page = self.page - self.plusend_page = self.page + self.pluspage_str_list = []# 首页self.query_dict.setlist(self.page_param, [1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>')# 上一页if self.page > 1:self.query_dict.setlist(self.page_param, [self.page-1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')for page_num in range(start_page, end_page + 1):if page_num == self.page:self.query_dict.setlist(self.page_param, [page_num])page_ele = f'<li class="active"><a href="?{self.query_dict.urlencode()}">{page_num}</a></li>'else:self.query_dict.setlist(self.page_param, [page_num])page_ele = f'<li><a href="?{self.query_dict.urlencode()}">{page_num}</a></li>'page_str_list.append(page_ele)# 下一页if self.page < self.page_count:self.query_dict.setlist(self.page_param, [self.page + 1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Next"><span aria-hidden="true">»</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>')# 尾页self.query_dict.setlist(self.page_param, [self.page_count])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>')# 将列表当中的字符串传递给html,并且一标签的形式展示search_page = """<li><form method="get" style="float: left;margin-left: 1px"><input type="text" class="form-control" placeholder="页码" name="page" style="position: relative;display: inline-block;width: 75px"><button class="btn btn-success btn-sm" type="submit">跳转</button></form></li>"""page_str_list.append(search_page)page_string = mark_safe("".join(page_str_list))return page_string


分页模块化封装

  • python%20package" style="background-color:transparent;">在app当中创建一个python package

  • 在当前包里面创建py文件

    # -*- coding:utf-8 -*-
    from django.utils.safestring import mark_safe
    from copy import deepcopy
    ​
    class PageData(object):def __init__(self, request, queryset, page_size=10, plus=2, page_param="page"):"""request: 请求queryset:数据表查询的结果page_size:每页展示多少条数据plus:当前页,前后各展示多少个页码"""get_query_dict = deepcopy(request.GET)self.query_dict = get_query_dictself.page_param = page_param# 分页page = request.GET.get(page_param, "1")  # 当前所在的页面# 判断字符创当中全部都是数字if page.isdecimal():page = int(page)else:page = 1
    ​self.page = pageself.start = (page - 1) * page_sizeself.end = page * page_size# 分页操作self.page_queryset = queryset[self.start: self.end]page_count = queryset.count()page_count, div = divmod(page_count, page_size)if div:page_count += 1self.page_count = page_countself.plus = plus
    ​def page_html(self):if self.page_count <= 2 * self.plus + 1:start_page = 1end_page = self.page_countelse:# 当前选中的页数小于等于三if self.page <= self.plus:start_page = 1end_page = 2 * self.plus + 1else:# 当前页 + 2if (self.page + self.plus) > self.page_count:start_page = self.page_count - self.plus * 2end_page = self.page_countelse:start_page = self.page - self.plusend_page = self.page + self.plus
    ​page_str_list = []# 首页page_str_list.append(f'<li><a href="?page=1" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>')
    ​# 上一页if self.page > 1:page_str_list.append(f'<li><a href="?page={self.page - 1}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')
    ​for page_num in range(start_page, end_page + 1):if page_num == self.page:page_ele = f'<li class="active"><a href="?page={page_num}">{page_num}</a></li>'else:page_ele = f'<li><a href="?page={page_num}">{page_num}</a></li>'page_str_list.append(page_ele)# 下一页if self.page < self.page_count:page_str_list.append(f'<li><a href="?page={self.page + 1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>')
    ​# 尾页page_str_list.append(f'<li><a href="?page={self.page_count}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>')# 将列表当中的字符串传递给html,并且一标签的形式展示search_page = """<li><form method="get" style="float: left;margin-left: 1px"><input type="text" class="form-control" placeholder="页码" name="page" style="position: relative;display: inline-block;width: 75px"><button class="btn btn-success btn-sm" type="submit">跳转</button></form></li>"""page_str_list.append(search_page)page_string = mark_safe("".join(page_str_list))return page_string

 


参数共存

  • 在做搜索的过程当中,翻页的时候,搜索的条件还在

    在翻页的时候,始终展示搜索出来满足条件的数据

    get_query_dict = deepcopy(request.GET)# 在get请求当中,固定存放一个page,有其他参数,不会被替换get_query_dict.setlist("page", [1])# 将两个参数使用&符号拼接起来# ?page=1&search=100print(get_query_dict.urlencode())
    # 搜索的时候,出现的参数http://127.0.0.1:8000/asset/data/?search=100
    # 做翻页的时候,搜索参数还在http://127.0.0.1:8000/asset/data/?search=100&page=3

--无论怎么搜索,查找,都只显示一个参数. 

  • 完整代码

    # -*- coding:utf-8 -*-
    from django.utils.safestring import mark_safe
    from copy import deepcopy
    ​
    class PageData(object):def __init__(self, request, queryset, page_size=10, plus=2, page_param="page"):"""request: 请求queryset:数据表查询的结果page_size:每页展示多少条数据plus:当前页,前后各展示多少个页码"""get_query_dict = deepcopy(request.GET)self.query_dict = get_query_dictself.page_param = page_param# 分页page = request.GET.get(page_param, "1")  # 当前所在的页面# 判断字符创当中全部都是数字if page.isdecimal():page = int(page)else:page = 1
    ​self.page = pageself.start = (page - 1) * page_sizeself.end = page * page_size# 分页操作self.page_queryset = queryset[self.start: self.end]page_count = queryset.count()page_count, div = divmod(page_count, page_size)if div:page_count += 1self.page_count = page_countself.plus = plus
    ​def page_html(self):if self.page_count <= 2 * self.plus + 1:start_page = 1end_page = self.page_countelse:# 当前选中的页数小于等于三if self.page <= self.plus:start_page = 1end_page = 2 * self.plus + 1else:# 当前页 + 2if (self.page + self.plus) > self.page_count:start_page = self.page_count - self.plus * 2end_page = self.page_countelse:start_page = self.page - self.plusend_page = self.page + self.plus
    ​page_str_list = []# 首页self.query_dict.setlist(self.page_param, [1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>')
    ​# 上一页if self.page > 1:self.query_dict.setlist(self.page_param, [self.page-1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')
    ​for page_num in range(start_page, end_page + 1):if page_num == self.page:self.query_dict.setlist(self.page_param, [page_num])page_ele = f'<li class="active"><a href="?{self.query_dict.urlencode()}">{page_num}</a></li>'else:self.query_dict.setlist(self.page_param, [page_num])page_ele = f'<li><a href="?{self.query_dict.urlencode()}">{page_num}</a></li>'page_str_list.append(page_ele)# 下一页if self.page < self.page_count:self.query_dict.setlist(self.page_param, [self.page + 1])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Next"><span aria-hidden="true">»</span></a></li>')else:page_str_list.append(f'<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>')
    ​# 尾页self.query_dict.setlist(self.page_param, [self.page_count])page_str_list.append(f'<li><a href="?{self.query_dict.urlencode()}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>')# 将列表当中的字符串传递给html,并且一标签的形式展示search_page = """<li><form method="get" style="float: left;margin-left: 1px"><input type="text" class="form-control" placeholder="页码" name="page" style="position: relative;display: inline-block;width: 75px"><button class="btn btn-success btn-sm" type="submit">跳转</button></form></li>"""page_str_list.append(search_page)page_string = mark_safe("".join(page_str_list))return page_string


http://www.ppmy.cn/devtools/46317.html

相关文章

黄仁勋的AI时代:英伟达GPU革命的狂欢与挑战

在最近的COMPUTEX 2024大会上&#xff0c;英伟达创始人黄仁勋发布了最新的Blackwell GPU。这次发布不仅标志着英伟达在AI领域的又一次飞跃&#xff0c;也展示了其对未来技术发展的战略规划。本文将详细解析英伟达最新技术的亮点&#xff0c;探讨其在AI时代的市场地位和未来挑战…

探索Python爬虫:实战演练,打造你的数据采集利器

在这个信息爆炸的时代&#xff0c;数据成为了最宝贵的资源之一。Python&#xff0c;以其简洁的语法和强大的库支持&#xff0c;成为了数据采集和处理的首选语言。本文将带领你走进Python爬虫的世界&#xff0c;通过一系列实战演练&#xff0c;教你如何构建自己的数据采集工具。…

Vue:网络请求axios

Axios 是一个基于 Promise 的网络请求库。 安装 npm install axios引入 <script setup> import axios from axios </script>常用 API axios.get(url[, config]) axios.get(/user,{params: {id: 1234,} }) .then(response > console.log(response)) .catch(…

python-bert模型基础笔记0.1.02

python-bert模型基础笔记0.1.00 bert的适合的场景bert多语言和中文模型bert模型两大类官方建议模型模型中名字的含义标题bert系列模型包含的文件bert系列模型参数微调与迁移学习区别参考链接bert的适合的场景 裸跑都非常优秀,句子级别(例如,SST-2)、句子对级别(例如Multi…

弘君资本股市资讯:“20cm”跌停!这些个股重挫,什么原因?

多家收到2023年年报问询函的A股上市公司&#xff0c;今天上午股价团体重挫&#xff0c;多股盘中甚至“20cm”跌停。 记者发现&#xff0c;在A股上市公司2023年年报发表后&#xff0c;交易所近期对上市公司密布宣布年报问询函&#xff0c;到目前&#xff0c;交易所对上市公司20…

MapReduce学习之MapJoin案例实现

MapReduce学习之MapJoin案例实现 1.当前main方法所在的入口类 package com.shujia.mr.mapJoin;import com.shujia.mr.reduceJoin.ReduceJoin; import com.shujia.mr.reduceJoin.ReduceJoinMapper; import com.shujia.mr.reduceJoin.ReduceJoinReducer; import org.apache.had…

【python】修改目标检测的xml标签(VOC)类别名

需求&#xff1a; 在集成多个数据集一同训练时&#xff0c;可能会存在不同数据集针对同一种目标有不同的类名&#xff0c;可以通过python脚本修改数据内的类名映射&#xff0c;实现统一数据集标签名的目的。 代码&#xff1a; # -*- coding: utf-8 -*- # Time : 2023/9/11 1…

vue 按钮权限

想要不同的用户拥有不同的功能权限 使用v-has"search:mdtj:add&#xff08;v-has中的字符自己编辑&#xff0c;一般是目录页面具体按钮功能&#xff09; 例如&#xff1a; <a-button click"handleAdd" type"primary" icon"plus" v-ha…