Django - implement paging

Keywords: Python Django Back-end

Django - implement paging

View core code

# Get all articles
post = Post.objects.all()
# Gets the value of p, which defaults to 1
p = request.GET.get('p', 1)
# The first parameter is a required parameter, which represents the data to be paged. The parameter value can be list, tuple or ORM query data object.
# The second parameter is a required parameter, which sets the amount of data on each page. The parameter value must be integer
paginator = Paginator(post, 4)
try:
    # Page (): sets the parameter object according to the current number of pages_ List, get the data information corresponding to the number of pages, and call the function return value_ get_page(). 
    pages = paginator.page(p)
except PageNotAnInteger:
    pages = paginator.page(1)
except EmptyPage:
    # num_pages(): get the total number of pages after paging.
    pages = paginator.page(paginator.num_pages)

Front end core code

  <nav class="pagination" style="">
	<ul>
        //has_previous(): judge whether the current page number has the previous page
        {% if pages.has_previous %}
        //previous_page_number(): if the current page number has a previous page, the page number of the previous page will be output; otherwise, an EmptyPage exception will be thrown.
        <li class="next-page"><a href="?p={{ pages.previous_page_number }}">previous page</a></li>
        {% endif %}
	  <li class="prev-page"></li>
        //page_range(): generate the total number of pages after paging into a recyclable object.
    {% for page in pages.paginator.page_range  %}
        {% if pages.number == page %}
	  <li class="active"><span>{{ page }}</span></li>

        {% elif pages.number|add:'-1' == page or pages.number|add:'1' == page %}
            <li><a href="?p={{ page }}">{{ page }}</a></li>
        {% endif %}
    {% endfor %}
        //has_next(): judge whether the current page number has the next page.
    {% if pages.has_next %}
        //next_page_number(): if the current page number has the next page, the next page number will be output; otherwise, an EmptyPage exception will be thrown.
	  <li class="next-page"><a href="?p={{ pages.next_page_number }}">next page</a></li>
    {% endif %}
	</ul>
  </nav>

Paginator class defines 4 initialization parameters and 8 class methods. The description of each initialization parameter and class method is as follows:

  • object_list: required parameter, representing the data to be paged. The parameter value can be list, tuple or ORM query data object.
  • per_page: required parameter to set the data volume of each page. The parameter value must be integer.
  • Orphans: optional parameter. If the data amount of the last page is less than or equal to the value of orphans, the data of the last page will be merged into the data of the previous page. For example, there are 23 rows of data, if the parameter per_ If page = 10 and orphans=5, the total number of pages after data paging is 2. The first page displays 10 lines of data and the second page displays 13 lines of data.
  • allow_empty_first_page: optional parameter, whether the first page is allowed to be empty. If the parameter value is False and the parameter object_ If the list is empty, an EmptyPage error will be raised.
  • validate_number(): verify that the current number of pages is greater than or equal to 1.
  • get_page(): call validate_number() verifies whether the current number of pages is valid, and the return value of the function calls page().
  • Page (): sets the parameter object according to the current number of pages_ List, get the data information corresponding to the number of pages, and call the function return value_ get_page(). _
  • get_page(): call the page class, pass the current number of pages and the data information corresponding to the number of pages to the page class, and create the data object of the current number of pages.
  • count(): get the parameter object_ The data length of the list.
  • num_pages(): get the total number of pages after paging.
  • page_range(): generate the total number of pages after paging into a recyclable object.
  • _ check_object_list_is_ordered(): if the parameter object_list is the data object queried by ORM, and the data of the data object is arranged out of order, a warning message will be prompted.

Get defined from Paginator class_ page(), page(), and_ get_page() knows that there is a calling relationship between the three

The definition process of Page class defines 3 initialization parameters and 7 class methods. The description of each initialization parameter and class method is as follows:

  • object_list: required parameter, representing the sliced data object.
  • Number: required parameter, representing the number of pages passed by the user.
  • Paginator: required parameter, representing the instantiated object of Paginator class.
  • has_next(): judge whether the current page number has the next page.
  • has_previous(): judge whether the current page number has the previous page.
  • has_other_pages(): judge whether the current page number has the previous page or the next page.
  • next_page_number(): if the current page number has the next page, the next page number will be output; otherwise, an EmptyPage exception will be thrown.
  • previous_page_number(): if the current page number has a previous page, the page number of the previous page will be output; otherwise, an EmptyPage exception will be thrown.
  • start_index(): output the position of the first row of data of the current number of pages in the whole data list, and the data position is calculated from 1.
  • end_index(): output the position of the last row of data of the current page number in the whole data list, and the data position is calculated from 1.

Posted by d~l on Tue, 09 Nov 2021 22:14:25 -0800