Previous words:
1. The Web page references the table in the bootstrap, so you need to introduce a style class
2. The first time to write articles, do not like to spray. There is insufficient place, can leave a message I correct, in this first thanked.
HTML code:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Paging </title>
<link rel= "stylesheet" href= "/static/bootstrap/css/bootstrap.min.css" >
<body>
<div class= "Container" >
<table class= "Table table-bordered" >
<thead>
<tr>
<th> Serial Number </th>
<th> numbering </th>
<th> name </th>
</tr>
</thead>
{% for user in user_list%}
<tr>
<td>{{Forloop.counter}}</td>
<td>{{user.id}}</td>
<td>{{User.Name}}</td>
</tr>
{% ENDFOR%}
</table>
<nav aria-label= "page navigation" >
<ul class= "Pagination" >
{{Page_html|safe}} <!--value--! >
</ul>
</nav>
</div>
</body>
Views Code:
From django.shortcuts import Render, redirect, HttpResponse
From APP01 import Models
# Create your views here.
def page (Request):
# Take parameters from URL
Page_num = Request. Get.get ("P")
# Ask for total pages, 10 data per page
Total_data = models. Books.objects.all (). Count ()
Total_page, i = Divmod (Total_data, ten) # Divmod operations are preferable to integers and remainder saved in Total_page,i
Try
page_num = Int (page_num)
If Page_num > Total_page:
Page_num = Total_page
Except Exception as E:
Page_num = 1
# define two variables to save the starting data
Print (Page_num)
Data_start = (page_num-1) *10 + 1
Data_end = page_num*10
If I:
Total_page = total_page + 1 # To determine the number, plus 1 pages
# How many pages are displayed on the page
Max_page = 7
If Total_page <= max_page:
Max_page = Total_page
Half_max_page = max_page//2
# Where do the page numbers start?
Page_start = Page_num-half_max_page
# Where does the page show up?
Page_end = Page_num + half_max_page
# front Section
If Page_start <= 1:
Page_start = 1
Page_end = Max_page
# after part
If Page_end > Total_page:
Page_end = Total_page
Page_start = total_page-max_page + 1
# self-splicing pagination httml code
Html_str_list = []
# Plus Home
Html_str_list.append (' <li><a href= '/page/?p=1 > Home </a></li> ')
# Add a previous page
If Page_num <= 1:
Html_str_list.append (
' <li class= ' disabled "><a href=" # "aria-label=" Previous "><span aria-hidden=" true ">«< /span></a></li> ')
Else
Html_str_list.append (
' <li><a href= '/page/?p={0} ' aria-label= ' Previous ' ><span aria-hidden= ' true ' >«</span ></a></li> '. Format (
PAGE_NUM-1))
For I in range (Page_start, page_end+1):
# Add an active style class if it is the current page
if i = = Page_num:
tem = ' <li class= ' active ' ><a href= '/page/?p={0} ' >{0}</a></li> '. Format (i)
Else
tem = ' <li><a href= '/page/?p={0} ' >{0}</a></li> '. Format (i) #括号里要写0
Html_str_list.append (TEM)
# Plus next page
If Page_num >= total_page:
Html_str_list.append (
' <li class= ' disabled ' ><a href= ' # ' aria-label= ' Next ' > ' <span aria-hidden= ' true ' >»</ Span></a></li> ')
Else
Html_str_list.append (
' <li><a href= '/page/?p={0} ' aria-label= ' Next ' ><span aria-hidden= ' true ' >»</span> </a></li> '. Format (
Page_num + 1))
# Plus last page
Html_str_list.append (' <li><a href= '/page/?p={0} ' > Last </a></li> '. Format (total_page))
page_html = "". Join (Html_str_list)
# All_user = models. UserInfo.objects.all () [10:20]
All_user = models. Books.objects.filter (Id__gte=data_start, Id__lte=data_end)
Print (All_user)
return render (Request, "page.html", {"user_list": All_user, "page_html": page_html})
HTML-based Python for paging functionality