If too many pages, it will show all looked very jumbled on a page
You can display the number of pages specified in the page
E.g:
book_list.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>书籍列表</title> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> </head> <body> <div class="container"> <table class="table table-bordered"> <thead> <tr> <th>序号</th> <th>id</th> <th>书名</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.id }}</td> <td>{{ book.title }}</td> </tr> {% endfor %} </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li> {{ page_html|safe }} </li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </body> </html>
views.py:
Import the render django.shortcuts from from app01 Import Models DEF book_list (Request): # parameters taken from the URL The page_num = request.GET.get ( "Page") Print (The page_num, type (The page_num)) The page_num = int (The page_num) # define two variables hold data taken from where to where DATA_START = (page_num-1) * 10 data_end page_num * = 10 # the total number of books total_count = models.Book.objects.all (). COUNT () # show how many of each page data per_page = 10 # total number of pages required to display total_page, m = divmod (total_count, per_page) on page # page show the most MAX_PAGE = 11 half_max_page // 2 = MAX_PAGE displayed on the page number of the start page # page_start = page_num - half_max_page Show on # page number of the end of the page PAGE_END = page_num + half_max_page # If the current page Less than half of 1 small IF PAGE_START <= 1: PAGE_START = 1 PAGE_END = MAX_PAGE # If the current page plus half than the total number of pages is greater if page_end> total_page : PAGE_END = total_page PAGE_START = total_page - MAX_PAGE # if there is data IF m: total_page + =. 1 all_book = models.Book.objects.all () [DATA_START: data_end] # splicing html paging codes html_list = [] for I in Range (PAGE_START, PAGE_END +. 1): . tmp = '<Li> <a href="/book_list/?page={0}"> </a> {0} </ Li>' the format (I) html_list. append (tmp) page_html = "" .join (html_list) return render(request, "book_list.html", {"books": all_book, "page_html": page_html})
operation result: