1.urls.py
url(r'^page2/',views.page2),
2.views.py
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
def page2(request):
customer_list=models.UserType.objects.all()
paginator=Paginator(customer_list,10)
page = request.GET.get('p')
try:
customer_objs=paginator.page(page)
except PageNotAnInteger:
customer_objs=paginator.page(1)
except EmptyPage:
customer_objs=paginator.page(paginator.num_pages)
return render(request,'page2.html',{'customer_list':customer_objs})
3.page2.html
{% load custom_tags %}
<style>
.active{
background: gold;
}
</style>
<table>
<thead>
<tr>
<th>ID</th>
<th>name</th>
</tr>
</thead>
<tbody>
{% for item in customer_list %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.caption }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<div class="pagination">
<nav>
{% if customer_list.has_previous %}
<span class="#"><a href="?p={{ customer_list.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">«</span></a></span>
{% endif %}
{% for page_num in customer_list.paginator.page_range %}
{% guess_page customer_list.number page_num %}
{% endfor %}
{% if customer_list.has_next %}
<span class="#"><a href="?p={{ customer_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">»</span></a></span>
{% endif %}
</nav>
</div>
4.自定义模板函数
app01/templatetags/custom_tags.py
from django import template
from django.utils.html import format_html
register=template.Library()
@register.filter
def _lower(val):
print('-----val',val)
return val.lower()
@register.simple_tag
def guess_page(current_page,loop_num):
offset=abs(current_page-loop_num)
if offset < 3:
if current_page == loop_num:
page_ele='<span class="active"><a href="?p=%s">%s</a></span>'%(loop_num,loop_num)
else:
page_ele='<span class=""><a href="?p=%s">%s</a></span>'%(loop_num,loop_num)
return format_html(page_ele)
else:
return ""

django【原生分页】的更多相关文章

  1. 第二十一章 Django的分页与cookie

    第二十一章 Django的分页与cookie 第一课 模板 1.模板的继承 在Template目录下新建模板master.html <!DOCTYPE html> <html lan ...

  2. Python Django的分页,Form验证,中间件

    本节内容 Django的分页 Form 中间件 1 Django 分页 1.1 Django自带的分页 1.首先来看下我的测试数据环境 ############ models.py ######### ...

  3. Django----列表分页(使用Django的分页组件)

    目的:是为了实现列表分页 1.定制URL http://127.0.0.1:8000/blog/get_article?page=3之前定制URL是在url后增加了/id,这次使用参数的方式 def ...

  4. django原生sql查询如何返回字典格式

    django原生sql查询,默认返回的是元祖.如果想返回字典格式,需要自行封装: http://www.360doc.com/content/17/0802/11/9200790_676042880. ...

  5. Django—自定义分页

    分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该显示在页面上的数据在数据库表中的起始位置. 确定分页需求: 1. 每页显示的数据条数 2. 每页显示页号链接数 3. 上一页 ...

  6. reverse(两种反向生成url django原生形式和rest_framework中版本的形式)

    reverse(两种反向生成url django原生形式和rest_framework中版本的形式) views.py from django.shortcuts import render,Http ...

  7. 使用Ajax (put delete ) django原生CBV 出现csrf token解决办法

    原因 django原生CBV中对于 Ajax put 或 delete 请求进行封装时,会把请求数据放在 request.body里, 所以获取不到csrf token 方式一: 关闭csrf 中间件 ...

  8. Django自定义分页并保存搜索条件

    Django自定义分页并保存搜索条件 1.自定义分页组件pagination.py import copy class Pagination: def __init__(self, current_p ...

  9. Django DRF 分页

    Django DRF 分页 分页在DRF当中可以一共有三种,可以通过setttings设置,也可也通过自定义设置 PageNumberPagination 使用URL http://127.0.0.1 ...

  10. django之分页,纯python代码

    Django中分页 py文件代码 """ 自定义分页组件 可以返回分页的数据和分页的HTML代码 """ from django.http ...

随机推荐

  1. 什么是KBEngine

    本文转自:https://github.com/harmy/kbengine 资源下载地址:http://sourceforge.net/projects/kbengine/files/ 什么是KBE ...

  2. Linux 设备驱动的固件载入

    作为一个驱动作者, 你可能发现你面对一个设备必须在它能支持工作前下载固件到它里面. 硬件市场的很多地方的竞争是如此得强烈, 以至于甚至一点用作设备控制固件的 EEPROM 的成本制造商都不愿意花费. ...

  3. 【Selenium】之谷歌、IE、火狐浏览器各个版本的浏览器驱动下载地址

    地址:chromedriver官网下载地址: http://chromedriver.storage.googleapis.com/index.html(失效了) http://npm.taobao. ...

  4. 第二百一十节,jQuery EasyUI,SearchBox(搜索框)组件

    jQuery EasyUI,SearchBox(搜索框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 SearchBox(搜索框)组件的使用方法,这个组 ...

  5. 【vijos】1892 树上的最大匹配(树形dp+计数)

    https://vijos.org/p/1892 这个必须得卡评测机+手动开栈才能卡过QAQ 手动开栈我百度的... int size=256<<20; //256MB char *p=( ...

  6. 【vijos】1750 建房子(线段树套线段树+前缀和)

    https://vijos.org/p/1750 是不是我想复杂了.... 自己yy了个二维线段树,然后愉快的敲打. 但是wa了两法.......sad 原因是在处理第二维的更新出现了个小问题,sad ...

  7. 2014年辛星解读css第一节

    CSS是Cascading Style Sheets的缩写.即层叠样式表,它用于表现HTML的样式,即HTML仅仅是去写该网页有哪些内容,至于怎样去表现它们,由CSS去定制. ************ ...

  8. ZeroMQ一个更小、更快、更简单的智能传输层协议

    这个githube上的教程是非常好的,是个中文翻译,大家直接学这个就行 https://github.com/anjuke/zguide-cn/tree/master/bin 原文地址: https: ...

  9. TF-IDF(词频-逆向文件频率)用于文字分类

    SVM分类器:支持向量机Support Vector Machine. 一个普通的SVM就是一条直线,用来完美划分linearly separable的两类.解决线性 要解决非线性需要到高维处理: 核 ...

  10. Django model :add a non-nullable field 'SKU' to product without a default; we can't do that

    You are trying to add a non-nullable field 'SKU' to product without a default; we can't do that (the ...