Django-custom page, django page

Source: Internet
Author: User
Tags blank page

Django-custom page, django page

 

The paging function is necessary for each website. for paging, it is used to calculate the starting position of the data that should be displayed on the page in the database table based on the user input.

Determine paging requirements:

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

:

First, use django's built-in paging function to write paging classes:

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: param current_page: Current page 9: param all_count: Total number of pages 10: param base_url: Template 11: param per_page: number of data entries per page 12: param show_page: Number of displayed link pages 13 "" 14 # If the url is incorrect, the first page is displayed by default (error type may be: blank page number, non-integer page number) 15 try: 16 self. current_page = int (cu R1__page) 17 failed t Exception as e: 18 self. current_page = 119 20 # obtain the total number of pages 21 a, B = divmod (all_count, per_page) 22 if B: 23 a + = 124 self. all_page = a 25 26 self. base_url = base_url27 self. per_page = per_page28 self. show_page = show_page29 30 # current page start data id31 def start_data (self): 32 return (self. current_page-1) * self. per_page33 34 # current page end data id35 def end_data (self): 36 return self. current_page * Self. per_page37 38 # dynamically generate the frontend html39 def pager (self): 40 page_list = [] 41 half = int (self. show_page-1)/2) 42 # if: the total number of pages <show_page, the default display page number range is: 1 ~ Total number of pages 43 if self. all_page <self. show_page: 44 start_page = 145 end_page = self. all_page + 146 # If: total page number> show_page47 else: 48 # If: current_page-half <= 0, the default page number range is 1 ~ Show_page49 if self. current_page <= half: 50 start_page = 151 end_page = self. show_page + 152 else: 53 # If: current_page + half> total number of pages, the default display page number range is: Total number of pages-show_page ~ Total number of pages 54 if self. current_page + half> self. all_page: 55 end_page = self. all_page + 156 start_page = end_page-self. show_page57 else: 58 start_page = self. current_page-half59 end_page = self. current_page + half + 160 61 # homepage 62 first_page = "<li> <a href = '% s? Page = % s'> homepage </a> </li> "% (self. base_url, 1) 63 page_list.append (first_page) 64 65 # previous page (if the current page is equal to the first page, there is no link on the previous page; otherwise, the link is 1 on the current page) 66 if self. current_page <= 1: 67 prev_page = "<li> <a href = '#'> previous page </a> </li>" 68 else: 69 prev_page = "<li> <a href = '% s? Page = % s'> previous page </a> </li> "% (self. base_url, self. current_page-1) 70 page_list.append (prev_page) 71 72 # dynamically generate intermediate Page Link 73 for I in range (start_page, end_page): 74 if I = self. current_page: 75 temp = "<li class = 'active'> <a href = '% s? Page = % s'> % s </a> </li> "% (self. base_url, I, I) 76 else: 77 temp = "<li> <a href = '% s? Page = % s'> % s </a> </li> "% (self. base_url, I, I) 78 page_list.append (temp) 79 80 # Next page (if the current page is the last page, there is no link to the next page; otherwise, the link is the current page plus 1) 81 if self. current_page> = self. all_page: 82 next_page = "<li> <a href = '#'> next page </a> </li>" 83 else: 84 next_page = "<li> <a href = '% s? Page = % s'> next page </a> </li> "% (self. base_url, self. current_page + 1) 85 page_list.append (next_page) 86 87 # last page (if there is only one page in total, there is no last page tag) 88 if self. all_page> 1: 89 last_page = "<li> <a href = '% s? Page = % s'> last page </a> </li> "% (self. base_url, self. all_page) 90 page_list.append (last_page) 91 92 return ''. join (page_list)

Then, write the method in views (this is written in app01 ):

1 from utils. pagnition import PageInfo # import the custom paging module 2 3 def custom (request): 4 all_count = models. userInfo. objects. all (). count () # obtain the total number of data entries of the database to be displayed 5 page_info = PageInfo (request. GET. get ('page'), all_count, '/custom.html/',) # generate the paging object 6 user_list = models. userInfo. objects. all () [page_info.start_data (): page_info.end_data ()] # retrieve the data displayed on the current page using the paging object 7 return render (request, 'm.m.html ', {'user _ list': user_list, 'page _ info': page_info}) # template Rendering

Then, write custom.html in the templatesdirectory:

1 <! DOCTYPE html> 2 

Finally, add the url link (urls. py ):

1 from django.conf.urls import url2 from django.contrib import admin3 from app01 import views as app01_views4 5 urlpatterns = [6     url(r'^custom.html/$', app01_views.custom),7 ]

So far, we have used django's paging function to customize the paging module, which can be applied to different business pages.

 

References:

1. The path to Python [Article 17th]: Django [advanced]

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.