程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django15: paging

編輯:Python

1、 Definition

         Pagination refers to paging in web The page has a lot of data to display , For the convenience of reading, only part of the data is displayed in each page . The advantage is that it is easy to read and reduce the amount of data extraction , Reduce server pressure .

        Django Provides Paginator Class can easily implement the paging function .Paginator Class is located ’django.core.paginator’ Module .

2、Paginator

         Responsible for the overall management of paging data .

(1)Paginator Object construction method :

paginator = Paginator(object_list, per_page)

Parameters :

  • object_list: List of objects that need paging data
  • per_page: Number of data per page

Return value :

Paginator The object of

(2)Paginator attribute

  • count: Total number of objects requiring paging data
  • num_pages: Total number of pages paged
  • page_range: from 1 At the beginning range object , Used to record the current code number
  • per_page: Number of data per page

(3)pagintor object .page(number)

Parameters number Page number information ( from 1 Start );

Returns the current number Also corresponding page information ;

If the page number provided does not exist , Throw out Invalidpage abnormal , Contains two unusual subclasses :

  • PageNotAnlnteger: Direction page() Thrown when a value other than an integer is passed in .
  • EmptyPage: Direction page() Provide a valid value , But it is thrown when there is no object on that page

3、page object

(1) Definition

         Manage a page of data .

(2) Create objects

        Paginator Object's page() Method returns page object .

page = Paginator.page( Page number )

(3)Page Object properties

  • object_list: List of all data objects on the current page
  • number: The serial number of the current page , from 1 Start
  • paginator: At present page Object related Paginator object

(4)Page Object methods

  • has_next(): If there is a next page, return to True
  • has_previout(): If there is a previous page, return to True
  • has_other_pages(): If there is a previous or next page, return to True
  • next_page_number(): Go back to the next page , If the next page doesn't exist , Throw out InvalidPage abnormal
  • previous_page_number(): Go back to the previous page , If the previous page does not exist , Throw out InvalidPage abnormal

example :
(a)views.py

def test_page(request):
# /test_page/4
# /test_page?page=1
page_num = request.GET.get('page', 1)
all_data = ['a', 'b', 'c', 'd', 'e']
# initialization paginator
paginator = Paginator(all_data, 2)
# Initialize the specific page number page object
c_page = paginator.page(int(page_num))
return render(request, 'test_page.html', locals())

(b)urls.py

urlpatterns = [
path('admin/', admin.site.urls),
# path('test_mw/', views.test_mw),
path('test_csrf', views.test_csrf),
path('test_page', views.test_page),
]

(c)template.test_page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Pagination </title>
</head>
<body>
<!-- <a href="/make_page_csv?page={
{ c_page.number }}"> Generate </a> -->
{% for p in c_page %}
<p>{
{ p }}</p>
{% endfor %}
{% if c_page.has_previous %}
<a href="/test_page?page={
{ c_page.previous_page_number }}"> The previous page </a>
{% else %}
The previous page
{% endif %}
{% for p_num in paginator.page_range %}
{% if p_num == c_page.number %}
{
{ p_num }}
{% else %}
<a href="/test_page?page={
{ p_num }}">{
{ p_num }}</a>
{% endif %}
{% endfor %}
{% if c_page.has_next %}
<a href="/test_page?page={
{ c_page.next_page_number }}"> The next page </a>
{% else %}
The next page
{% endif %}
</body>
</html>

visit :http://192.168.28.128:8000/test_page


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved