django has a built-in paging, although the function is very comprehensive, but not suitable for my application scenarios, it is to write a code that does not take away thanks!
Application scenarios:
1 is preferably used django
Instructions:
To the data (quesset types of data, to jump to the page, stitching path) returns a dictionary returns Why dictionary? Direct return convenient front-end for the short term I use, and if there are other needs that you can change it
Where to use: a direct reference}} {{load
Renderings:
Page rosy effect of using drama, is behind the style is not fine, I'm not a professional, you tune about it
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 "" " 2 function call is started my_html () . 3 parameters are needed . 4 param que: a data type querryset . 5 new_num_page: jump to page number . 6 the href: stitching path . 7 . 8 . 9 " "" 10 DEF HTML (new_lis, new_num_page, the page_num, the href): . 11 "" " 12 is : param new_lis: 13 is : param new_num_page: 14 : param the page_num: 15 : param the href:? incoming path of joining such as / custorm / Page = 16 : return: . 17 "" " 18 is page_html = " " 19 page_pre_html = f'<nav aria-label="Page navigation"><ul class="pagination "><li><a href="{href}1" aria-label="Previous"><span aria-hidden="true">首页</span></a></li><li><a href="{href}{new_num_page - 1}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>' 20 page_html += page_pre_html 21 for i in new_lis: 22 if i == str(new_num_page): 23 page_html += f'<li ><a href="{href}{i}" style="color:red" >{i}</a></li>' 24 else: 25 page_html += f'<li ><a href="{href}{i}" >{i}</a></li>' 26 27 pagenum_html = f'<li><a href="{href}{new_num_page + 1}" aria-label="Next"><span aria-hidden="true" >»</span></a></li><li><a href="{href}{page_num}" aria-label="pattern"><span aria-hidden="true">尾页</span></a></li><li><span aria-hidden="true" ><form action="" method="get" ><input type="text" style="width:80px;height:18px;" placeholder="共:{page_num}页" name="page" ><input type="submit" style="width:80px;height:18px;" value="跳转"></form></li></ul></nav>' DEF32income data, prepares dictionary spread out#3130page_html return29pagenum_html page_html + =28 my_html (que, new_num_page, the href, page_max_piece = 10, page_tag_num =. 5): # param que: a querry type of data new_num_page: New Page href: stitching path 33 is "" " 34 is : param que: a querry type data 35 : param new_num_page: new page 36 the href: stitching path 37 [ : param page_max_piece: the maximum number of page displays 38 is : param page_tag_num: number of pages of the page displayed is preferably an odd number not easily changed 39 : return: return a good slice carries dictionary the querry html page and a pre-stage can be returned directly by the 40 "" " 41 is all_data_count = que.count () 42 is the page_num, RESID = divmod (all_data_count, page_max_piece) # quotient and remainder 43 is IF RESID: 44 page_num += 1 # 拿到了总页数 45 page_all_lis = [str(i) for i in range(1, page_num + 1)] 46 if new_num_page in page_all_lis: 47 new_num_page = int(new_num_page) 48 if new_num_page > 2 and new_num_page < page_num - 1: 49 ret = html(page_all_lis[new_num_page-3:new_num_page+2],new_num_page,page_num,href) 50 elif new_num_page <= page_tag_num: 51 ret = html(page_all_lis[ 0:page_tag_num], new_num_page, page_num,href) 52 elif new_num_page > page_num-2: 53 ret = html(page_all_lis[page_num-page_tag_num:page_num],new_num_page,page_num,href) 54 return {"que": que[(new_num_page - 1) * page_max_piece:new_num_page * page_max_piece], "new_html": ret} 55 else: 56 new_num_page=1 57 ret = html(page_all_lis[0:page_tag_num], new_num_page, page_num,href) 58 return {"que": que[(new_num_page - 1) * page_max_piece:new_num_page * page_max_piece], "new_html": ret}