1. Django paginator 1. View
From Django. shortcuts import render, httpresponse # create your views here. from app01.models import * from Django. core. paginator import paginator, emptypage, pagenotanintegerdef index (request): ''' batch import data: booklist = [] For I in range (100): Booklist. append (Book (Title = "book" + STR (I), price = 30 + I * I) book. objects. use of the bulk_create (booklist) ''''' page sharer: book_list = book. objects. all () paginator = paginator (book_list, 10) print ("count:", paginator. count) # print ("num_pages", paginator. num_pages) # print ("page_range", paginator. page_range) # page number list page1 = paginator. page (1) # Page Object on page 1st for I in page1: # print (I) print (page1.object _ List) # page2 = paginator. page (2) print (page2.has _ next () # whether the next page has print (page2.next _ page_number () # print (page2.has _ previous ()) # Whether the previous page has print (page2.previous _ page_number () # The previous page number # throwing an error # page = paginator. page (12) # error: emptypage # page = paginator. page ("Z") # error: pagenotaninteger ''' book_list = book. objects. all () paginator = paginator (book_list, 10) page = request. get. get ('page', 1) currentpage = int (PAGE) Try: Print (PAGE) book_list = paginator. page (PAGE) Comment t pagenotaninteger: book_list = paginator. page (1) Partition t emptypage: book_list = paginator. page (paginator. num_pages) return render (request, "index.html", {"book_list": book_list, "paginator": paginator, "currentpage": currentpage })
2.index.html:
<! Doctype HTML> <HTML lang = "en"> Ii. Expansion
Def index (request): book_list = book. objects. all () paginator = paginator (book_list, 15) page = request. get. get ('page', 1) currentpage = int (PAGE) # if there are too many pages, use another display method if paginator. num_pages> 11: If currentPage-5 <1: pagerange = range () Elif currentpage + 5> paginator. num_pages: pagerange = range (currentPage-5, paginator. num_pages + 1) else: pagerange = range (currentPage-5, currentpage + 5) else: pagerange = paginator. page_range try: Print (PAGE) book_list = paginator. page (PAGE) Comment t pagenotaninteger: book_list = paginator. page (1) Partition t emptypage: book_list = paginator. page (paginator. num_pages) return render (request, "index.html", locals ())
Django component-page sharer