Django 中的自定义分页标签
目录结构:

1.在应用下,migrations的同级目录创建templatetags目录(主要两个文件,包含__init__.py)
2.创建分页标签(pagetag.py)
#!/usr/bin/env python
# _*_ coding:utf-8 _*_ from django import template
from django.utils.html import format_html # 注册标签
register = template.Library()
@register.simple_tag
def circle_page(curr_page,loop_page):
offset = abs(curr_page - loop_page)
if offset < 3:
if curr_page == loop_page:
page_ele = '<li class="active"><a href="?page=%s">%s</a></li>'%(loop_page,loop_page)
else:
page_ele = '<li><a href="?page=%s">%s</a></li>'%(loop_page,loop_page)
return format_html(page_ele)
else:
return ''
3.在view.py中将数据分页处理
# 数据分析
def analysisMovie(request):
movies = AnalysisMovie.objects.all().filter(is_delete='')
# 分页
paginator = Paginator(movies, 25, 2) # Show 25 contacts per page
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
context = {'contacts': contacts}
return render(request,'analysis.html',context=context)
4.在url.py中设置指向
url(r'^analysis/$', views.analysisMovie),
5.在analysis.html中数据展示(部分)
{% load pagetag %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数据分析</title>
<!-- 全局样式 -->
<link rel="stylesheet" href="/static/css/global.css">
<!-- 导航菜单样式 -->
<link rel="stylesheet" href="/static/navigation.css">
<!-- 数据展现表格样式 -->
<link rel="stylesheet" href="/static/table.css">
<!-- bootstrap样式 -->
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css">
<script type="text/javascript" src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="panel-body">
<form>
<div class="input-group col-md-3" style="margin-top:0px;positon:relative">
<input type="text" class="form-control" placeholder="请输入明星"/>
<span class="input-group-btn">
<button class="btn btn-info btn-search">查找</button>
<button class="btn btn-info btn-search" style="margin-left:3px">添加</button>
</span>
</div>
<table id="hor-minimalist-a" align="center">
<tr>
<th>演员</th>
<th>电影名称</th>
<th>评分</th>
<th>类型</th>
<th>评论数</th>
</tr>
{% for movie in contacts %}
<tr>
<td>
{{ movie.actor_name }}
</td>
<td>
{{ movie.movie_name }}
</td>
<td>
{% if movie.movie_mark == 0 %}
暂无评分
{% else %}
{{ movie.movie_mark }}分
{% endif %}
</td>
<td>
{{ movie.movie_type }}
</td>
<td>
{% if movie.movie_rating_num == '' %}
暂无评论
{% else %}
{{ movie.movie_rating_num }}
{% endif %}
</td>
</tr>
{% endfor %}
</table>
<!-- 分页 -->
<nav aria-label="Page navigation">
<ul class="pagination">
{% if contacts.has_previous %}
<li>
<a href="?page={{ contacts.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
<a href="?page=1" aria-label="First">
<span aria-hidden="true">首页</span>
</a>
</li>
{% endif %}
{% for pg in contacts.paginator.page_range %}
{% circle_page contacts.number pg %}
{% endfor %}
{% if contacts.has_next %}
<li>
<a href="?page={{ contacts.paginator.num_pages }}" aria-label="Last">
<span aria-hidden="true">尾页</span>
</a>
<a href="?page={{ contacts.next_page_number }}" aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
{% endif %}
</li>
</ul>
</nav>
</form>
</div>
6.效果


Django 中的自定义分页标签的更多相关文章
- Struts2自定义标签4自定义分页标签
第一步:webroot/web-inf下的str.tld文件 <?xml version="1.0" encoding="UTF-8"?> < ...
- Django中的自定义过滤器
一.为什么要自定义Django中的自定义过滤器:Django中提供了很多内置的过滤器和标签,详见链接django官网,主要有以下几个: autoescape(自动转义)block(模板继承)csrf_ ...
- 第三百一十四节,Django框架,自定义分页
第三百一十四节,Django框架,自定义分页 自定义分页模块 #!/usr/bin/env python #coding:utf-8 from django.utils.safestring impo ...
- Java自定义分页标签的实现
主要字段含义: 页号 pagaNo页面大小 pageSize总记录条数 recordCount计算本次一共分多少页 myPageSize页号显示开始 start 页号显示结束 end PageTag需 ...
- 自定义分页标签,并使分页标签能获得url中的参数
如题,要实现一个分页功能,其次,要让分页标签“智能一点”,在分页时能自动带上url后面的参数 <tag> <description>分页标签</description&g ...
- python 全栈开发,Day87(ajax登录示例,CSRF跨站请求伪造,Django的中间件,自定义分页)
一.ajax登录示例 新建项目login_ajax 修改urls.py,增加路径 from app01 import views urlpatterns = [ path('admin/', admi ...
- (转)java web自定义分页标签
转载至http://liuxi1024.iteye.com/blog/707784 效果如图: 1.JSP规范1.1版本后增加了自定义标签库.实现自定义标签的步骤 (1)开发自定义标签处理类. (2) ...
- django cookie session 自定义分页
cookie cookie的由来 http协议是无状态的,犹如人生若只如初见,每次都是初次.由此我们需要cookie来保持状态,保持客户端和服务端的数据通信. 什么是cookie Cookie具体指的 ...
- Django模板之自定义过滤器/标签/组件
自定义步骤: 1. 在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 2. 在app应用中创建templatet ...
随机推荐
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
- ss - float浮动模块的高度问题 解决方案
当一个Div中的子元素都是浮动元素时,该div是没有高度的.通常会带来很多困扰,解决方案如下: 低版本统配兼容: overflow: hidden; 下面是不支持低配浏览器,而且似乎该效果对 P 标签 ...
- redis conf 详解
2.8配置 # Redis configuration file example # Note on units: when memory size is needed, it is possible ...
- [k8s]k8s架构图解
k8s架构图解 启动参数及证书梳理 master端必须要装flannel 注: flannel网络能确保各节点间 Pod 网段实现互通 master 节点与 node 节点上的 Pods 通过 Pod ...
- makefile之findstring函数
#$(findstring <find>,<in> ) #功能:在字串<in>中查找<find>字串. #返回:如果找到,那么返回<find> ...
- html-文本处理
文本处理-相关操作: <!-- 申明为html5版本 --> <!DOCTYPE html> <html> <head> <title>文本 ...
- qt中执行 sql文件的方法
由于qt中没有原生的执行sql文件的方法.因此我们需要根据sql文件中的流的特点,将其分解成一个个语句单独执行. 1.首先通过Qfile读取sql文件 2.将sql文件中的内容通过“:”进行拆解 3. ...
- session用户账号认证(一个用户登陆,踢出前一个用户)
在web.xml中配置: <listener> <listener-class>cn.edu.hbcf.common.listener.SessionAttributeList ...
- hive2.1.1配置
hive2.1.1配置 首先在conf下复制hive-site.xml <property> <name>javax.jdo.option.ConnectionURL</ ...
- Unix系统编程(二)open的练习
在上一篇中学习了一下open函数的基本用法,这里就要练习一下.计算机就是要实践嘛. 用open创建一个文件 #include <sys/types.h> #include <sys/ ...