Ii. Paginator
1 >>> from django. core. paginator import Paginator 2 >>> objects = ['C # ', 'java', 'python', 'javascript ', 'php'] 3> p = Paginator (objects, 2) # One page splitter with two data entries per page 4> p. count # total data 5 5 6> p. num_pages # Total number of pages 7 3 8 >>> p. page_range # page number List 9 [1, 2, 3] 10 >>> page1 = p. page (1) # 1st Page 11 >>> page112 <page 1 of 3> 13 >>> page1.object _ list # data on Page 14 ['C #', 'java'] 15 >>> page2 = p. page (2) # 2nd Page 16 >>> page217 <page 2 of 3> 18 >>> page2.object _ list # data on Page 19 ['python ', 'javascript '] 20 >>> page2.has _ next () # whether there is a next page 21 True22 >>> page2.has _ previous () # Whether there is a previous page 23 True24 >>> page2.has _ other_pages () # whether there are other pages 25 True26 >>> page2.next _ page_number () # page 27 328> page2.previous _ page_number () # page 29 130> page2.start _ index () # The ordinal number of the first record on this page (starting from 1) 31 332 >>>> page2.end _ index () # The ordinal number of the last record on this page (starting from 1) 33 434> p. page (0) # error page, throwing an exception 35... emptyPage: That page number is less than 136 >>> p. page (3) # error page, throwing an exception 37... emptyPage: That page contains no results
Next we will use pagination in the view function.
Def listing (request): "" Function Description: display on pages --------------------------------------------------------------------------- the reason why the modifier modified the time was admin 2013-04-16 "news =. objects. all (). order_by ('-id') paginator = Paginator (news, 5) # Show 5 news per page # Make sure that the paging request is an integer. If not, deliver the first page. try: page = int (request. GET. get ('page', 1) failed t VauleError: page = 1 # If the paging request exceeds the range, deliver the last page try: contacts = paginator. page (page) Metadata T (EmptyPage, InvalidPage): contacts = paginator. page (paginator. num_pages) return render_to_response('list.html ', {'contacts': contacts })
Before using it, remember to import:
From django. core. paginator import Paginator, EmptyPage, InvalidPage # import the paging Model
After url Configuration:
(R' ^ listing/$ ', 'login. views. listing'), # Paging
Next, write the template:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Now, start the server and view the results in the browser: