Django component-page sharer

Source: Internet
Author: User
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

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.