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 count start_page = page_num - 1 if page_num > 1 else 1 end_page = page_num + 3 if (max_page_num - page_num) >= 3 else max_page_num for 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_safe page_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