django 分页 带搜索框
生活随笔
收集整理的這篇文章主要介紹了
django 分页 带搜索框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from django.utils.safestring import mark_safe"""
自定義的分頁組件def pretty_list(request):from app01.utils.pagination import Pagination# 1.根據自己的情況去篩選自己的數據queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")# 2.實例化分頁對象page_object = Pagination(request, queryset)context = {"search_data": search_data,"queryset": page_object.page_queryset, # 分完頁的數據"page_string": page_object.html() # 頁碼}return render(request, "pretty_list.html", context)
# 在html中<ul class="pagination">{{ page_string }}</ul>
"""class Pagination(object):def __init__(self, request, queryset, page_size=10, page_param="page", plus=5):""":param request: 請求的對象:param queryset: 查詢符合條件的數據,根據這個數據對他進行分頁處理:param page_size: 每頁顯示多少條數據:param page_param: 在url中傳遞的獲取分頁的參數 例如: /pretty/list/?page=12:param plus: 顯示當前頁的前后幾頁(頁碼)"""from django.http.request import QueryDictimport copyquery_dict = copy.deepcopy(request.GET)query_dict._mutable = Trueself.query_dict = query_dictpage = request.GET.get(page_param, "1")if page.isdecimal():page = int(page)else:page = 1self.page = pageself.page_param = page_paramself.page_size = page_sizeself.start = (self.page - 1) * self.page_sizeself.end = self.page * self.page_sizeself.page_queryset = queryset[self.start: self.end]self.plus = plus# 數據總條數total_count = queryset.count()# 計算總頁碼數total_page_count, div = divmod(total_count, page_size) # divmod 除法 第二個是余數if div > 1:total_page_count += 1if total_page_count != 0:self.total_page_count = total_page_countelse:self.total_page_count = 1def html(self):# 計算出當前頁的前5頁, 后5頁plus = 5if self.total_page_count < 2 * self.plus + 1:# 數據庫中數據比較少,都沒有達到11頁start_page = 1end_page = self.total_page_countelse:# 數據庫中數據比較多 > 11頁# 當前頁小于5時(小極值)if self.page <= self.plus:start_page = 1end_page = 2 * self.plus + 1else:# 當前頁大于5# 當前頁+5 > 總頁面if (self.page + 5) > self.total_page_count:start_page = self.total_page_count - 2 * self.plusend_page = self.total_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('<li><a href="?{}">首頁</a></li>'.format(self.query_dict.urlencode()))# 上一頁if self.page > 1:self.query_dict.setlist(self.page_param, [self.page - 1])prey = '<li><a href="?{}">上一頁</a></li>'.format(self.query_dict.urlencode())else:self.query_dict.setlist(self.page_param, [1])prey = '<li><a href="?{}">上一頁</a></li>'.format(self.query_dict.urlencode())page_str_list.append(prey)for i in range(start_page, end_page + 1):if i == self.page:self.query_dict.setlist(self.page_param, [i])ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)else:self.query_dict.setlist(self.page_param, [i])ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)page_str_list.append(ele)# 下一頁if self.page < self.total_page_count:self.query_dict.setlist(self.page_param, [self.page + 1])prey = '<li><a href="?{}">下一頁</a></li>'.format(self.query_dict.urlencode())else:self.query_dict.setlist(self.page_param, [self.total_page_count])prey = '<li><a href="?{}">下一頁</a></li>'.format(self.query_dict.urlencode())page_str_list.append(prey)# 尾頁self.query_dict.setlist(self.page_param, [self.total_page_count])page_str_list.append('<li><a href="?{}">尾頁</a></li>'.format(self.query_dict.urlencode()))search_string = """<li><form style="float: left; margin-left: -1px" method="get"><input type="text" name='page' style="position: relative;float: left;display: inline-block;width: 80px;border-radius: 0"class="form-control" placeholder="頁碼"><button style="border-radius: 0" class="btn btn-default" type="submit">跳轉</button></form></li>"""page_str_list.append(search_string)page_string = mark_safe("".join(page_str_list))return page_string
總結
以上是生活随笔為你收集整理的django 分页 带搜索框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [RCAR][显示]选型
- 下一篇: Photoshop中图层叠加效果的算法