注意,我将templates定义在项目的同级目录下:

在settings.py中配置

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

urls.py

from django.urls import path
from . import views app_name='person'
urlpatterns=[
path('test/', views.test),
path('test/<int:pn>',views.test,name='test'),
]

views.py

from django.shortcuts import render
from .models import Book
from django.core.paginator import Paginator def test(request,pn=1):
#获取所有的查询
book_obj=Book.objects.all()
#传给paginator,每页显示两条
paginator=Paginator(book_obj,2)
#pn是显示第几页,默认是第一页
page=paginator.page(pn)
#将page通过context传给前端
context={'page':page}
return render(request,'test/test.html',context=context)

models.py

class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=128,null=False) def __str__(self):
return "book_title:{}".format(self.title)

tempates/test/test.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
li{
float: left;
list-style: none;
margin-left: 5px;
}
</style>
</head>
<body>
<div>
<div style="position: absolute;top: 35%;left: 40%;">
<table border="">
<thread>
<tr>
<th>id</th>
<th>title</th>
</tr>
</thread>
<tbody>
{% for item in page %}
<tr>
<td width="120px">{{item.id}}</td>
<td width="120px">{{item.title}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!--底部分页按钮显示-->
<div style="position: absolute;top: 50%;left: 44%"
<nav aria-label="Page navigation">
<div class="pagination">
<ul class="pagination" >
{% if page.has_previous %}
<li><a href="/test/{{page.previous_page_number}}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span></a></li>
{% endif %} {% for num in page.paginator.page_range%}
{%if pindex == page.number%}
<li><a href="">{{ num }}</a></li>
{%else%}
<li><a href="/test/{{num}}">{{ num }}</a></li>
{%endif%}
{% endfor %} {% if page.has_next %}
<li><a href="{% url 'person:test' page.next_page_number %}" aria-label="Next">
<span aria-hidden="true">&raquo;</span></a></li>
{% endif %}
</ul>
</div>
</nav>
</div>
</body>
</html>

最终效果(不要在意css,不大美观,哈哈)

在显示下网页源代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
li{
float: left;
list-style: none;
margin-left: 5px;
}
</style>
</head>
<body>
<div>
<div style="position: absolute;top: 35%;left: 40%;">
<table border="">
<thread>
<tr>
<th>id</th>
<th>title</th>
</tr>
</thread>
<tbody> <tr>
<td width="120px">3</td>
<td width="120px">java</td>
</tr> <tr>
<td width="120px">6</td>
<td width="120px">zabbix从入门到精通</td>
</tr> </tbody>
</table>
</div>
<!--底部分页按钮显示-->
<div style="position: absolute;top: 50%;left: 44%"
<nav aria-label="Page navigation">
<div class="pagination">
<ul class="pagination" > <li><a href="/test/1" aria-label="Previous">
<span aria-hidden="true">&laquo;</span></a></li>
<li><a href="/test/1">1</a></li>
<li><a href="/test/2">2</a></li>
<li><a href="/test/3">3</a></li>
<li><a href="/test/3" aria-label="Next">
<span aria-hidden="true">&raquo;</span></a></li> </ul>
</div>
</nav>
</div>
</body>
</html>

总结:这是实现分页最简单的了,至于美观,可以结合bootstrap来进行美化。

技术总结:最基本的是Paginator里面的一些值(当然此处我并没有去尝试其他的,有兴趣的可以去搜下,也挺简单的)。然后其中的一个就是前端pn值如何通过url传值给后端,注意标蓝的地方。

学习不易,且学且珍惜!!!

实战--dango自带的分页(极简)的更多相关文章

  1. .NET Core实战项目之CMS 第七章 设计篇-用户权限极简设计全过程

    写在前面 这篇我们对用户权限进行极简设计并保留其扩展性.首先很感谢大家的阅读,前面六章我带着大家快速入门了ASP.NET Core.ASP.NET Core的启动过程源码解析及配置文件的加载过程源码解 ...

  2. .NET Core实战项目之CMS 第八章 设计篇-内容管理极简设计全过程

    写在前面 上一篇文章中我带着大家进行了权限部分的极简设计,也仅仅是一个基本的权限设计.不过你完全可以基于这套权限系统设计你的更复杂的权限系统,当然更复杂的权限系统要根据你的业务来进行,因为任何脱离实际 ...

  3. 【转】手摸手,带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板)

    前言 做这个 vueAdmin-template 的主要原因是: vue-element-admin 这个项目的初衷是一个vue的管理后台集成方案,把平时用到的一些组件或者经验分享给大家,同时它也在不 ...

  4. 串口助手下载-带时间戳的串口助手-极简串口助手-V1.1 自动保存配置参数 能显示收发时间方便调试

    1.串口助手下载 2.带时间戳的串口助手,每次收发指令带上了时间戳,方便调试 3.极简串口助手 4.简单易用 高速稳定 5.每次修改的参数都能自动保存,免去了重复配置的工作 下载地址:http://w ...

  5. .NET开源项目 QuarkDoc 一款自带极简主义属性的文档管理系统

    有些话说在前头 因为公司产品业务重构且功能拆分组件化,往后会有很多的接口文档需要留存,所以急需一款文档管理系统.当时选型要求3点: 1.不能是云平台上的Saas服务,整个系统都要在自己公司部署维护(数 ...

  6. Nginx 极简教程(快速入门)

    作者:dunwu github.com/dunwu/nginx-tutorial 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计模式内容聚合 4.  ...

  7. nginx极简教程

    Nginx 极简教程 本项目是一个 Nginx 极简教程,目的在于帮助新手快速入门 Nginx. examples 目录中的示例模拟了工作中的一些常用实战场景,并且都可以通过脚本一键式启动,让您可以快 ...

  8. 自制 os 极简教程1:写一个操作系统有多难

    为什么叫极简教程呢?听我慢慢说 不知道正在阅读本文的你,是否是因为想自己动手写一个操作系统.我觉得可能每个程序员都有个操作系统梦,或许是想亲自动手写出来一个,或许是想彻底吃透操作系统的知识.不论是为了 ...

  9. CSharpGL(28)得到高精度可定制字形贴图的极简方法

    CSharpGL(28)得到高精度可定制字形贴图的极简方法 回顾 以前我用SharpFont实现了解析TTF文件从而获取字形贴图的功能,并最终实现了用OpenGL渲染文字. 使用SharpFont,美 ...

随机推荐

  1. Android Studio [TextView]

    MainAcitvity package com.xdw.secondapp; import android.content.Intent;import android.os.Bundle;impor ...

  2. HNU_小初高数学学习软件_功能说明

    结对编程项目主要功能: 1.用户注册功能.用户提供手机号码,点击注册将收到一个注册码,用户可使用该注册码完成注册: 2.用户完成注册后,界面提示设置密码,用户输入两次密码匹配后设置密码成功.密码6-1 ...

  3. java 对数组进行截取组合操作

    1.使用skip跳过和limit限制组合,示例: Integer skip = (page.getPageNum() - 1) * page.getPageNum(); Integer limit = ...

  4. Python 爬虫入门实战

    1. 前言 首先自我介绍一下,我是一个做 Java 的开发人员,从今年下半年开始,一直在各大技术博客网站发表自己的一些技术文章,差不多有几个月了,之前在 cnblog 博客园加了网站统计代码,看到每天 ...

  5. 阿里云Centos操作Mysql

    1.  CentOS7 yum方式安装MySQL5.7 2.Centos下mysql导入sql 3.Centos设置远程 4.CentOS下mysql数据库常用命令总结 5.centos出现“Fire ...

  6. springboot结合mybatis

    idea中新建springboot项目 pom.xml依赖部分如下 <dependencies> <dependency> <groupId>org.springf ...

  7. docker相关概念介绍

    关键词:docker容器与docker镜像 他们之间的关系是docker容器通过docker镜像来创建 docker镜像就是些像ubuntu15.10,ubuntu14.2一样的系统 docker容器 ...

  8. js对数组、对象的深拷贝、复制

    基本类型的数据是存放在栈内存中的,而引用类型的数据是存放在堆内存中的 基本类型:Number Boolean undefined String Null 引用类型:Object Function js ...

  9. C#中的等值判断1

    目录 简介 值类型和引用类型的相等比较 和相等比较相关的函数 string 和 System.Uri 的等值比较 泛型接口 IEquatable<T> 自定义比较方法 举例 总结 简介 最 ...

  10. MacOS 导入MySQLdb 报错解决思路(解决ImportError: this is MySQLdb version (1, 2, 3, 'beta', 1), but _mysql is version (1, 2, 5, 'final', 1))

    cd /Library/Python/2.7/site-packages ls rm -rf MySQL_python-1.2.5-py2.7.egg-info 然后重新import 即可