路由配置
path(<span class="hljs-string">r'^stupage/<int:nowPage>/$'</span>,views.stuPage,name=<span class="hljs-string">'page'</span>),
视图
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">stuPage</span><span class="hljs-params">(req,nowPage)</span>:</span>
allStu = Students.stuObj.all()
pag = Paginator(allStu,<span class="hljs-number">6</span>)
<span class="hljs-keyword">if</span> int(nowPage)>=int(pag.num_pages):
nowPage = pag.num_pages
<span class="hljs-keyword">try</span>:
page = pag.page(nowPage)
<span class="hljs-keyword">except</span>:
page = pag.page(pag.num_pages)
<span class="hljs-comment"># print(page)</span>
<span class="hljs-keyword">return</span> render(request,<span class="hljs-string">'studentPage.html'</span>,{<span class="hljs-string">'students'</span>:page})
模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
展示分页的数据
{% for i in data.object_list %}
<p>{{ i.username }}</p>
{% endfor %}
展示分页的页码
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="{% url 'App:showPage' %}?page=1" aria-label="Next">
<span aria-hidden="true">首页</span>
</a>
</li>
<li {% if not data.has_previous %}class="disabled"{% endif %}>
<a {% if data.has_previous %}
href="{% url 'App:showPage' %}?page={{ data.previous_page_number }}"
{% endif %} aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% for index in data.paginator.page_range %}
<li {% if index == data.number %}class="active"{% endif %}><a href="{% url 'App:showPage' %}?page={{ index }}">{{ index }}</a></li>
{% endfor %}
<li {% if not data.has_next %}class="disabled"{% endif %}>
<a {% if data.has_next %}
href="{% url 'App:showPage' %}?page={{ data.next_page_number }}"
{% endif %} aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<li {% if not data.paginator.num_pages %}class="disabled"{% endif %}>
<a {% if data.paginator.num_pages %}
href="{% url 'App:showPage' %}?page={{ data.paginator.num_pages }}"
{% endif %} aria-label="Next">
<span aria-hidden="true">尾页</span>
</a>
</li>
</ul>
</nav>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>