In Django projects, paging is a very critical feature when dealing with large amounts of data. It not only improves the user experience, but also reduces the burden on the server. Django provides a powerful paging tool—, can easily realize pagination display of data. This article will introduce in detail how to use Django's paging plugin to implement the paging function.
1. Introduce a paging device
First, you need to introduce Django's pager module into your view file. This is the first step to implementing the paging function.
from import Paginator
2. Create a paging object
When creating a pager object, two parameters need to be passed in:
-
object_list
: Unpatched query results, usually a QuerySet object. -
per_page
: The number of entries displayed per page.
# Suppose we have a list of model objects query_setquery_set = () # Show 10 pieces of data per pageper_page = 10 # Create a paging objectpaginator = Paginator(query_set, per_page)
3. Set page numbers and get paging data
usepaginator.get_page(number)
Method, you can obtain paging data for specific page numbers. herenumber
It is the page number requested by the user.
# number is the page number requested by the user, for example, get it from the URL parameternumber = ('page', 1) # The first page is displayed by default # Get the page object after the pagepage = paginator.get_page(number)
4. Common properties and methods of Page object
Page
Objects contain many useful properties and methods that can help you implement paging logic and page navigation.
object_list: The data list of the current page.
current_data = page.object_list
number: Current page number.
current_page_number =
paginator.num_pages: Total number of pages.
total_pages = paginator.num_pages
has_next: Is there a next page?
has_next_page = page.has_next
has_previous: Is there a previous page?
has_previous_page = page.has_previous
next_page_number: The page number of the next page.
next_page_num = page.next_page_number if page.has_next else None
previous_page_number: The page number of the previous page.
previous_page_num = page.previous_page_number if page.has_previous else None
5. Render paging data in templates
In Django templates, you can passpage
Objects are used to render paging data and generate paging navigation links.
<!-- Render the data of the current page --> <ul> {% for item in page.object_list %} <li>{{ item.some_field }}</li> {% endfor %} </ul> <!-- Pagination Navigation --> <div> <span>Page {{ }} of {{ .num_pages }}</span> {% if page.has_previous %} <a href="?page=1" rel="external nofollow" >&laquo; first</a> <a href="?page={{ page.previous_page_number }}" rel="external nofollow" >previous</a> {% endif %} <span> {% for i in .page_range %} {% if == i %} <strong>{{ i }}</strong> {% else %} <a href="?page={{ i }}" rel="external nofollow" >{{ i }}</a> {% endif %} {% endfor %} </span> {% if page.has_next %} <a href="?page={{ page.next_page_number }}" rel="external nofollow" >next</a> <a href="?page={{ .num_pages }}" rel="external nofollow" >last &raquo;</a> {% endif %} </div>
6. Summary
Through the above steps, you can easily implement paging in your Django project. Pagination can not only improve user experience, but also effectively manage the efficiency of data loading. Using Django providedPaginator
Class, you can quickly build a fully functional pagination system. Whether it is a simple list page or a complex query result, the paging function can help you better display data.
This is the end of this article about the implementation example of Django paging operation. For more related Django paging operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!