Django uses Paginator for pagination

# Import the Paginator class
from django.core.paginator import Paginator
Build a list of tests
test_list = [i for i in range(0.10)]
Create a pager with 4 entries per page
p = Paginator(test_list, 4)
# check how many entries there are
p.count				# 10
# check how many pages there are in the range from 1 to 3
p.page_range		# range(1, 4)

# ------------- first page
Get the first page of data
page1 = p.page(1)
Print the current page
print(page1)		# <Page 1 of 4>
View the objects of the current page as a list
page1.object_list	# [0, 1, 2]
Print the contents of the current page using for
for i in page1:
    print(i)
    
# ------------- page 2
page2 = p.page(2)
Check to see if there is a previous page
page2.has_previous()			# True
# What page is the previous page
page2.previous_page_number()	# 1
Check to see if there is a next page
page2.has_next()				# True
What is the next page
page2.next_page_number()		# 3
# Data at the start of the current page
page2.start_index()				# 5
# End of current page data
page2.end_index()				# 8

# ------------- page 3
page3 = p.page(3)
Check to see if there is a next page
page3.has_next()				# False
Check to see if there is a previous page
page3.has_previous()			# True
# What page is the previous page
page3.previous_page_number()	# 2
# If there are other pages
page3.has_other_pages()			# True
Copy the code

Write a paginated back-end logic with the premise that there is currently a database with five pieces of customer data in the database and that two pieces of customer data are required to be displayed per page.

def guest_manage_with_pages(request) :
    Get all data
    guest_list = Guest.objects.all(a)Create a pager that displays 2 entries per page
    paginator = Paginator(guest_list, 2)
    Receive the page from the front end, and turn the page at the back end
    page = request.GET.get('page')
    Get the specified page data
    try:
        contacts = paginator.page(page)
    Parameter error returns the first page of data
    except PageNotAnInteger:
        contacts = paginator.page(1)
    # Return to the last page when the maximum number of pages is exceeded
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    Pass to the front end
    return render(request, 'guest_manage.html', {'user':username,'guests':contacts})
Copy the code

Front-end display logic

    <div class="pagination">
        <span class="step-links">
            <! -- If there is a previous page, display the previous page a link -->
            {% if guests.has_previous %}
            <a href="? page={{ guests.previous_page_number }}">The previous page</a>
            {% endif %}
            <! Print the current page -->
            <span class="current">
                <! -- Page {{ guests.number }} of {{ guests.paginator.num_pages }} -->
                {{ guests }}
            </span>
            <! -- If there is a next page, show the next page a link -->
            {% if guests.has_next %}
            <a href="? page={{ guests.next_page_number }}">The next page</a>
            {% endif %}
        </span>
    </div>
Copy the code

End result:

The first page:

The second page:

The third page: