项目文件:

  

models.py(建表)

 from django.db import models
# Create your models here. class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
date = models.DateField()
#执行makemigrations /migrate命令初始化数据库

models.py

test.py(添加数据)

 from django.test import TestCase

 # Create your tests here.
import os
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fenyetest.settings")
import django
django.setup() from app01 import models
import random
books_obj=[]
for i in range(1,101):
book_obj= models.Book(title=f'书籍{i}',price=random.randint(1,100)+1,date=f'2018-0{random.randint(1,9)}-0{random.randint(1,9)}')
books_obj.append(book_obj)
print(book_obj.__dict__) models.Book.objects.bulk_create(books_obj)

test.py

urls.py(路由分发)

 from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^show/', views.show, name='show'),
]

urls.py

views.py(试图函数)

 from django.shortcuts import render
from app01 import models
from django.core.paginator import Paginator,EmptyPage, PageNotAnInteger # Create your views here. def show(request):
book_obj = models.Book.objects.all()
paginator = Paginator(book_obj,8)#每页显示8条数据
print(paginator.count)#总数据条数
print(paginator.num_pages)#总页数
print(paginator.page_range)#页数范围 current_page_num=int(request.GET.get('page',1))#通过a标签的GET方式请求,默认显示第一页
book_objs=paginator.page(current_page_num)#获取当前页面的数据
if book_objs.has_previous():#当前页面是否有前一页
print(book_objs.previous_page_number())#当前页面的前一页页码
if book_objs.has_next():#当前页面是否有后一页
print(book_objs.next_page_number())#当前页面的后一页页码 page_range=paginator.page_range
if paginator.num_pages>5:#页码只显示5页,总页数小于5页时,直接全部显示
if current_page_num<3:
page_range=range(1,6)
elif current_page_num+2>paginator.num_pages:
page_range=range(paginator.num_pages-5,paginator.num_pages+1)
else:
page_range=range(current_page_num-2,current_page_num+3) return render(request, 'show.html', {'book_objs': book_objs,'page_range':page_range,'current_page_num':current_page_num})

views.py

templates(模板页面)

show.html

 {% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
<title>信息展示</title>
</head>
<body>
<div class="container ">
<div class="panel panel-primary">
<div class="panel-heading"><span>图书信息表</span></div>
<div class="panel-body">
<div class="row">
<div class="col-xs-6 text-left">
<a href="">
<button type="button" class="btn btn-info" id="insert">新增</button>
</a>
</div>
<div class="col-xs-3 col-xs-offset-3">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</span>
</div>
</div>
</div>
</div>
<!-- Table -->
<table class="table table-striped">
<thead>
<tr>
<th class="text-center">编号</th>
<th>书籍名称</th>
<th>价格</th>
<th>出版日期</th>
</tr>
</thead> <!-- 数据渲染 -->
<tbody id="tb">
{% for book_obj in book_objs %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.date|date:'Y-m-d' }}</td>
</tr>
{% endfor %}
</tbody> </table>
<!-- 页码控制渲染-->
<nav aria-label="Page navigation" class="pull-right">
<ul class="pagination">
<!-- 上一页 -->
<li>
{% if book_objs.has_previous %}
<a href="{% url 'show' %}?page={{ book_objs.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
{% else %}
<a href="{% url 'show' %}?page={{ current_page_num }}" aria-label="Previous" class="disabled">
<span aria-hidden="true">&laquo;</span>
</a>
{% endif %} </li>
<!-- 页码-->
{% for page_num in page_range %}
<li class="{% if current_page_num == page_num %}active{% endif %}"><a href="{% url 'show' %}?page={{ page_num }}" >{{ page_num }}</a></li>
{% endfor %}
<!-- 下一页 -->
<li>
{% if book_objs.has_next %}
<a href="{% url 'show' %}?page={{ book_objs.next_page_number }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
{% else %}
<a href="{% url 'show' %}?page={{ current_page_num }}" aria-label="Next" class="disabled">
<span aria-hidden="true">&raquo;</span>
</a>
{% endif %} </li>
</ul>
</nav> </div>
</div>
</body>
<script src="{% static 'jquery-3.4.1.js' %}"></script>
<script src="{% static 'jquery-cookie-1.4.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
</html>

show.html

最新文章

  1. AlloyTeam2015前端大会都说了啥
  2. EditText 显示明文和密码
  3. SACS +Petrel 2009地震
  4. CentOS 7 做服务器 CentOS 5 做客服机 搭建Apache+php+mysql网页
  5. C++中的预处理
  6. (转)SqlServer中处理每天四亿三千万记录的
  7. 异常:java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
  8. WCF 动态生成 不用增加引用两种方式
  9. 玩一个:可以显示任何xml树结构的xaml定义
  10. C# Datatable导出Excel方法
  11. 初始化Direct3D
  12. C#获取用户登录IP地址
  13. unittest测试用例的执行顺序
  14. Vs Code 插件配置教程
  15. Determine YARN and MapReduce Memory Configuration Settings
  16. Linux的记事本 Vi和Vim
  17. python点滴:读取和整合文件夹下的所有文件
  18. 【389】Implement N-grams using NLTK
  19. mysql升级到5.6源
  20. 理解JavaWeb项目中的路径问题——相对路径与绝对路径

热门文章

  1. OSG加载倾斜摄影数据
  2. JDK13的六大重要新特性
  3. Nginx SSL/HTTPS 配置
  4. [开发笔记]-unix时间戳、GMT时间与datetime类型时间之前的转换
  5. 使nfs同步生效
  6. 最小生成树之prime算法
  7. RobotFrameWork 自动化环境搭建(基于 python2.7)
  8. nginx常见应用实例
  9. 转载acm几何基础(2)
  10. Programming Languages_05 FWAE