Django--实现分页功能,并且基于cookie实现用户定制每页的数据条数
生活随笔
收集整理的這篇文章主要介紹了
Django--实现分页功能,并且基于cookie实现用户定制每页的数据条数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# page_num 當前頁數, total_result_num 總共有多少條測試結果
def pagination(request, page_num, total_result_num, result_per_page=10):""" 測試頁面的分頁 """page_num = int(page_num)# 點擊頁數page_index_list = []count, left = divmod(total_result_num, result_per_page)max_page_num = count + 1 if left > 0 else countstart_page = page_num - 1 if page_num > 1 else 1end_page = page_num + 3 if (max_page_num - page_num) >= 3 else max_page_numfor temp_page_num in range(start_page, end_page + 1):page_index_list.append('/result/' + str(temp_page_num))for i in range(len(page_index_list)):if i == page_num:page_index_list[i] = "<li class='active'><a href='%s'>%s</a></li>" % (page_index_list[i], start_page + i)else:page_index_list[i] = "<li><a href='%s'>%s</a></li>" % (page_index_list[i], start_page + i)# url list--> str --> mark_safepage_index = ' '.join(page_index_list)page_index = mark_safe(page_index)# 上一頁和下一頁last_page_index = "/result/" + str(page_num)next_page_index = "/result/" + str(page_num + 2)last_page_class = ''next_page_class = ''if page_num == max_page_num - 1:next_page_class = 'btn disabled'if page_num == 0:last_page_class = 'btn disabled'return page_index, last_page_index, last_page_class, next_page_index, next_page_class, max_page_num
轉載于:https://www.cnblogs.com/wangjiale1024/p/9807893.html
總結
以上是生活随笔為你收集整理的Django--实现分页功能,并且基于cookie实现用户定制每页的数据条数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电影《让子弹飞》
- 下一篇: 如何使用MiniProfiler(附最新