django pagination:
from django.shortcutsimportrender
from django.core.paginator import Paginator,EmptyPage, PageNotAnInteger
from app01 import models
# Pages and the current page of data objects object's methods can be found in the template to property ( without parentheses ) method call
= models.Book.objects.all book_obj () # Get the current books all the information
= Paginator the paginator (book_obj, . 8 ) # instantiated tab objects per page . 8 pieces of data
Print (paginator.count) # The total number of data
Print (paginator.num_pages) # Pages
Print (paginator.page_range) # page range
= current_page_num int (request.GET.get ( 'Page' , . 1 )) # by a tag GET mode request, the first page of the default display
= paginator.page book_objs (current_page_num) # Get the current page of data objects for rendering the display in response to a request distal
IF book_objs.has_previous (): # current page if there are previous page
Print (book_objs.previous_page_number ()) # current page previous page
IF book_objs.has_next (): # current page if there after a
Print (book_objs.next_page_number ()) # the current page after a page
try:
print(page)
book_objs = paginator.page(page)
except PageNotAnInteger:
book_objs = paginator.page(1)
except EmptyPage:
book_objs = paginator.page(paginator.num_pages)
# Set the page of the page number of display operation (at the front end is a need for a method of using the current page of data objects)
= paginator.page_range page_range # determine the range of pages to display the page template rendering operation
IF paginator.num_pages> . 5 : # Page Displays . 5 pages, total number of pages is less than . 5 page appears, all displayed directly
if current_page_num<3:
page_range=range(1,6)
elif current_page_num+2>paginator.num_pages:
page_range=range(current_page_num-5,paginator.num_pages+1)
else:
page_range=range(current_page_num-2,current_page_num+3)
return render(request, 'show.html', {'book_objs': book_objs,'page_range':page_range,'current_page_num':current_page_num})