Python__django Sub-page

Source: Internet
Author: User

To customize the paging class:

#!/usr/bin/env python#-*-coding:utf-8-*-#Created by Mona on 2017/9/20 fromDjango.utils.safestringImportMark_safeclassPaginator:" "the format of the page number depends on the bootstrap; Use case: from django.shortcuts import render,redirect,httpresponse from App01.mod Els Import * from Tools Import page Imports Def index (request): Base_url = Request.path_info Total _item_count = UserInfo.objects.all (). Count () current_page = Int (request. Get.get (' page ', 1)) Page_obj = page. Paginator (total_item_count,current_page,base_url) user_list = UserInfo.objects.all () [Page_obj.start:page_obj.en D] return render (Request, ' index.html ', {' user_list ': user_list, ' html_page ': Page_obj.pager})" "    def __init__(Self,total_item_count,current_page,base_url,
per_page_item_count=10,show_pager_count=11): " ":p Aram Total_item_count: Total number of records:p Aram Current_page: Current page:p aram base_url: Prefix URL for page number:p Aram Per_page_item_count: Number of records displayed per page:p Aram Show_pager_count: Number of page numbers displayed" "Self.total_item_count=Total_item_count self.current_page=current_page Self.base_url=Base_url Self.per_page_item_count=Per_page_item_count Self.show_pager_count=Show_pager_count Total_page, res=Divmod (Self.total_item_count, Self.per_page_item_count)ifRes:total_page+ = 1Self.total_page=total_page Self.half_show_pager_count= Int (TOTAL_PAGE/2) @propertydefStart (self):" "page number query data start location: return:" " return(self.current_page-1) *Self.per_page_item_count @propertydefEnd (self):" "end of page query data: return:" " returnself.current_page*Self.per_page_item_count @propertydefPager (self):" "return the required front page number: return:" "page_list=[] UL='<ul class= "pagination" >'Page_list.append (UL)ifSelf.current_page = = 1: Prev='<li><a href= "#" > Prev </a></li>' Else: Prev='<li><a href= "%s?page=%s" > Prev </a></li>'% (Self.base_url, self.current_page-1) page_list.append (prev)#If the data is particularly small: ifSelf.total_page <Self.show_pager_count:pager_start= 1Pager_end= Self.total_page + 1Else: ifSelf.current_page <=Self.half_show_pager_count:pager_start= 1Pager_end= Self.show_pager_count + 1Else: ifSelf.current_page + Self.half_show_pager_count >Self.total_page:pager_start= Self.total_page-self.show_pager_count + 1Pager_end= Self.total_page + 1Else: Pager_start= Self.current_page-Self.half_show_pager_count pager_end= self.current_page + Self.half_show_pager_count + 1 forIinchRange (Pager_start, pager_end):ifi = =SELF.CURRENT_PAGE:TPL='<li class= "active" ><a href= "%s?page=%s" >%s</a></li>'%(Self.base_url, I, I,)Else: TPL='<li><a href= "%s?page=%s" >%s</a></li>'%(Self.base_url, I, I,) page_list.append (TPL)ifSelf.current_page = =Self.total_page:nex='<li><a href= "#" > Next </a></li>' Else: Nex='<li><a href= "%s?page=%s" > Next </a></li>'% (Self.base_url, Self.current_page + 1,) Page_list.append (NEX) Lu='</ul>'Page_list.append (LU) html_page= Mark_safe ("'. Join (page_list))returnHtml_page

Effect Show:

Python__django Sub-page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.