page 怎么把list 分页_自定义分页器的实现
生活随笔
收集整理的這篇文章主要介紹了
page 怎么把list 分页_自定义分页器的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
django也有自帶額分頁器,但是功能較少,使用起來較為繁瑣。所以我們可以使用自定義非分頁器。自定義分頁器的推導思路
# queryset對象是支持索引切片操作的(不支持負數索引) book_queryset=models.Book.objects.all() page_queryset=book_queryset[page_obj.start:page_obj.end] ? # 如何獲取用戶訪問的頁數? get請求是可以攜帶參數:url?page 默認顯示的是第一頁:current_page=request.GET.get("page",1) "page"參數非法,則顯示默認的第一頁 ? # 設定每頁的展示的記錄數? per_page_num=10 ? # 切片的起始位置和結束位置 start_page=(current_page-1)*10 end_page=current_page*10 ? # 獲取展示數據的總條數 record_count=book_queryset.count() //通過queryset對象的count方法獲取數據庫數據的總條數 ? # 確定總的數據需要多少頁才能展示 內置方法divmod() base_page,is_carry=divmod(record_count,per_page_num) ? # 前端模板語法沒有range功能 前端代碼后端書寫,寫完傳給前端 ? # 針對展示頁碼的需要自己規劃好到底展示多少個頁碼 一般情況下頁碼的個數設計都是奇數(符合審美標準) 11個頁碼 當前頁減5 當前頁加6 你可以給標簽價樣式從而讓選中的頁碼高亮顯示 ? # 針對頁碼小于6的情況 你需要做處理 不能再減思路有了,那就擼起袖子寫吧。
封裝好的代碼
""" 自定義分頁器代碼 """ ? class Pager(object):def __init__(self, current_page, record_count, per_page_num=8, pager_count=11):"""current_page:當前頁record_count:數據庫中記錄的總條數per_page_num:每頁最多展示的條數pager_count: 分頁器同時展示的最大數"""self.current_page = self.page(current_page)self.record_count = record_countself.per_page_num = per_page_numself.pager_count = pager_countself.pager_count_half = int(pager_count) // 2 ?@staticmethoddef page(page):if not isinstance(page, int):page = int(page) if page.isdigit() else 1page = page if page > 1 else 1return page ?@propertydef all_page(self):base_page, is_carry = divmod(self.record_count, self.per_page_num)return base_page if not is_carry else (base_page + 1) ?@propertydef start(self):"""切片操作的初始索引"""return (self.current_page - 1) * self.per_page_num ?@propertydef end(self):"""切片操作的結束索引"""return self.current_page * self.per_page_num ?def page_html(self):# 如果總頁碼小于11個if self.all_page <= self.pager_count:pager_start = 1pager_end = self.all_page + 1else:"""總頁碼大于11個"""if self.current_page <= self.pager_count_half:# 當前頁小于等于5頁pager_start = 1pager_end = self.pager_count + 1else:# 當前頁大于5頁if self.current_page + self.pager_count_half > self.all_page:pager_start = self.all_page - self.pager_count + 1pager_end = self.all_page + 1else:pager_start = self.current_page - self.pager_count_halfpager_end = self.current_page + self.pager_count_half + 1page_html_list = ["""<nav aria-label="Page navigation"><ul class="pagination">"""]first_page = '<li><a href="?page=%s">首頁</a></li>' % (1)page_html_list.append(first_page)if self.current_page == 1:prev_page = '<li class="disabled " ><a href="#">%s</a></li>' % ('<<',)else:prev_page = '<li ><a href="?page=%s">%s</a></li>' % (self.current_page - 1, '<<',)page_html_list.append(prev_page)for item in range(pager_start, pager_end):if item == self.current_page:common_page = '<li class="active " ><a href="?page=%s">%s</a></li>' % (item, item,)else:common_page = '<li ><a href="?page=%s" >%s</a><li>' % (item, item,)page_html_list.append(common_page)if self.current_page >= self.all_page:next_page = '<li class="disabled " ><a href="#">%s</a></li>' % ('>>',)else:next_page = '<li ><a href="?page=%s">%s</a></li>' % (self.current_page + 1, '>>')page_html_list.append(next_page)last_page = '<li ><a href="?page=%s">尾頁</a></li>' % (self.all_page,)page_html_list.append(last_page)# 尾部添加標簽page_html_list.append("""</ul></nav>""")return ''.join(page_html_list)使用
注意:
""" 當我們需要使用到非django內置的第三方功能或者組件代碼的時候,一般情況下會創建一個名為utils文件夾,在該文件夾內對模塊進行功能性劃分,utils可以在每個應用下創建,需要具體結合實際情況。 ? 我們自定義的分頁器是基于bootstrap樣式來的,所以你需要提前導入bootstrap: (1)bootstrap 版本 v3 (2)jQuery 版本 v3 """ └── utils├── pager.py└── __pycache__└── pager.cpython-38.pycdjango后端:
class Book(View):def get(self, request, phone):user_obj = models.User.objects.filter(phone=phone).first()book_list = models.Book.objects.all()current_page = request.GET.get("page", 1)record_count = book_list.count()page_obj = pager.Pager(current_page, record_count, per_page_num=7, pager_count=11)page_queryset = book_list[page_obj.start:page_obj.end]return render(request, 'book.html',{'user': user_obj, 'page': page_obj, 'page_queryset': page_queryset})前端:
<div class="col-lg-6"><div class="input-group"><input type="text" class="form-control" placeholder="書名~"><span class="input-group-btn"><button class="btn btn-success" type="button">搜索~</button></span></div><!-- /input-group --></div><div class="col-lg-6 text-right"><a href="{% url 'app01:book_add' user.phone %}" class="btn btn-success">添加書籍</a></div><br><br><br><div class="col-lg-10 col-lg-offset-1"><table class="table table-bordered table-striped table-hover"><thead><tr><th class="text-center thead-style">序號</th><th class="text-center thead-style">書名</th><th class="text-center thead-style">價格(元)</th><th class="text-center thead-style">作者</th><th class="text-center thead-style">出版日期</th><th class="text-center thead-style">出版社</th><th class="text-center thead-style">操作</th></tr></thead><tbody>{% for book in page_queryset %}<tr><td class="text-center tbody-style">{{ page.start|add:forloop.counter }}</td><td>{{ book.name }}</td><td class="text-center">{{ book.price }}</td><td>{% for author in book.authors.all %}{% if forloop.last %}{{ author.name }}{% else %}{{ author.name }}|{% endif %}{% endfor %}</td><td class="text-right">{{ book.publish_date|date:'Y/m/d' }}</td><td class="text-center">{{ book.publish.name }}</td><td class="text-center"><a href="{% url 'app01:book_edit' user.phone book.id %}"class="btn btn-primary btn-xs">編輯</a><button class="btn btn-danger btn-xs del" delete_id="{{ book.pk }}">刪除</button></td></tr>{% endfor %}</tbody></table><div class="text-center">{{ page.page_html|safe }}</div></div>效果展示
效果圖1效果圖2總結
以上是生活随笔為你收集整理的page 怎么把list 分页_自定义分页器的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 换行符_一文看遍 JS 的所有输入
- 下一篇: 打印机打印网页不清晰_打印机墨水:你不知