Examples of pages that Django introduces

Source: Internet
Author: User
Paging is necessary for each site, and for paging, it is the user's input that calculates the starting position of the data that should be displayed on the page in the database table.

To determine paging requirements:

1. Number of data bars displayed per page 2. Page number links per page 3. Previous page and next page 4. Home and last

First, use the Django built-in paging feature to write the paging class:

 1 from Django.core.paginator Import paginator, page # import Django Paging Module 2 3 4 class PageInfo (object): 5 def __init_  _ (Self, current_page, All_count, Base_url, per_page=10, show_page=11): 6 "" "7 8:p Aram Current_page: Current page 9:p Aram All_count: Total pages:p Aram Base_url: Templates:p Aram Per_page: Number of data bars per page:p Aram Show_p Age: Displays the number of linked pages "" "#若url错误, the first page is displayed by default (the type of error may be: Empty page number, non-integer page number) try:16 Self.current_pag e = Int (current_page) except Exception as e:18 self.current_page = 119 #根据数据库信息条数 Total Pages A, B = Divmod (All_count, per_page) if b:23 A + = 124 Self.all_pag E = a self.base_url = base_url27 Self.per_page = Per_page28 Self.show_page = Show_     Page29 #当前页起始数据id31 def start_data (self): return (self.current_page-1) * Self.per_page33 34 #当前页结束数据id35 def End_data (self): $ self.current_page * self.per_page37 #动态生成前端html39 def Pager (self): 40         Page_list = []41 half = Int ((self.show_page-1)/2) #如果: Total pages < Show_page, default display page range: 1~ Total pages 43         If Self.all_page < self.show_page:44 start_page = 145 End_page = self.all_page + 146             #如果: Total pages > Show_page47 else:48 #如果: current_page-half <= 0, default display page range: 1~show_page49             If Self.current_page <= half:50 start_page = 151 End_page = self.show_page + 152 else:53 #如果: Current_page + Half > Total pages, default display page range: Total pages-show_page ~ Total pages, if SEL F.current_page + Half > self.all_page:55 end_page = self.all_page + 156 start_p                     Age = end_page-self.show_page57 else:58 start_page = self.current_page-half59 End_page = self.current_page + half + #首页62 first_page = "<li><a href= '%s?page=%s ' > Home </a>         </li> "% (Self.base_url, 1) page_list.append (first_page) #上一页 (if the current page equals the first page, the previous page has no link, otherwise the link is the current page minus 1) 66         If Self.current_page <= 1:67 prev_page = "<li><a href= ' # ' > previous </a></li>" 68 else:69 prev_page = "<li><a href= '%s?page=%s ' > Prev </a></li>"% (Self.base_url, SE lf.current_page-1) page_list.append (prev_page) #动态生成中间页数链接73 for I in range (Start_page, end _page): if i = = self.current_page:75 temp = "<li class= ' active ' ><a href= '%s?page=%s ' >%s</a></li> '% (Self.base_url, I, i) else:77 temp = "<li><a href= '% s?page=%s ' >%s</a></li> '% (Self.base_url, I, i) page_list.append (temp) #下一页 (if current page equals the last page, the next page has no link, otherwise the link is the current page plus 1) Bayi if self.current_page >= self.all_page:82 next_page = "<li><a href= ' # ' > Next </a> </li> "else:84 next_page =" <li><a href= '%s?page=%s ' > Next </a></li> "% (s Elf.base_url, self.current_page+1) page_list.append (next_page) #末页 (if the total number of pages is only one page, there is no end tag) if SE Lf.all_page > 1:89 last_page = "<li><a href= '%s?page=%s ' > End </a></li>"% (self.base_ URL, self.all_page) page_list.append (last_page) ". Join (Page_list)

Then, write the method in the Views (written here in App01):

1 from utils.pagnition import PageInfo    # import the step-up custom paging module from the file 2 3 def custom (Request): 4     All_count = models. UserInfo.objects.all (). Count ()   # Gets the total number of data bars to display the database 5     page_info = PageInfo (Request. Get.get (' page '), All_count, '/custom.html/',)      # Raw Content Page Object 6     user_list = models. UserInfo.objects.all () [Page_info.start_data ():p age_info.end_data ()]      # Get current page display data with paging object 7     return render ( Request, ' custom.html ', {' user_list ': user_list, ' Page_info ': Page_info})     # template rendering

Then, write the "custom.html" file in the Templates directory:

1 <! DOCTYPE html> 2 

Finally, add a URL relationship (urls.py):

1 from django.conf.urls import url2 from Django.contrib import admin3 from app01 import views as App01_views4 5 Urlpattern s = [6     url (r ' ^custom.html/$ ', App01_views.custom), 7]

This completes the custom paging module using Django's paging functionality, which can be applied on different business pages.

Resources:

1. Python route "17th": Django "Advanced article"

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.