为了防止XSS即跨站脚本攻击,需要加上 safe

# 路由
from django.conf.urls import url
from django.contrib import admin
from mypaginator import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hosts.html$', views.hosts),
] # 视图函数
def hosts(request):
"""创建测试数据"""
# for i in range(303):
# name = "c%s.com" % i
# port = i
# models.Host.objects.create(hostname=name,port=port)
# return HttpResponse("OK") # 默认每页显示10条数据
items_per_page = 10
# 前端请求的url:http://127.0.0.1:8001/hosts.html?page=2
current_page = int(request.GET.get('page'))
# x -> items_per_page*(x-1) ~ items_per_page*x
start_item = items_per_page*(current_page-1)
end_item = items_per_page*current_page
# 通过切片的形式从数据库中拿到对应页数的items
host_list = models.Host.objects.all()[start_item:end_item] page_str = """
<a href="hosts.html?page=1">1</a>
<a href="hosts.html?page=2">2</a>
<a href="hosts.html?page=3">3</a>
""" return render(request,'hosts.html',locals()) # 前端HTML
...
<div class="pagination">
{{ page_str|safe }}
</div>
...

  

自定义分页组件

# 路由

from django.conf.urls import url
from django.contrib import admin
from mypaginator import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hosts.html$', views.hosts),
]

  

# models

from django.db import models

# Create your models here.

class Host(models.Model):
hostname = models.CharField(max_length=32)
port = models.CharField(max_length=10)

  

# 分页组件:  ..\paginator\utils\paginator.py

#!/usr/bin/python
# -*- coding:utf-8 -*- class Paginator(object):
def __init__(self,all_count,current_page,base_url,per_page=10,per_page_count=11):
"""
分页组件的初始化
:param all_count: 数据的总条数
:param current_page: 当前页码
:param base_url: 基础url
:param per_page: 每页显示的条目数
:param per_page_count: 显示的页码数
"""
self.all_count = all_count
self.current_page = current_page
self.base_url = base_url
self.per_page = per_page
self.per_page_count = per_page_count
# 计算得出真实的页码数
pager_count, remainder = divmod(self.all_count, self.per_page)
if 0 != remainder:
pager_count += 1
self.pager_count = pager_count
# 默认每次显示11个页码(除上一页、下一页、首页和尾页之外)并且让当前选择页码始终居中
self.half_per_page_count = int(self.per_page_count / 2) @property
def start(self):
"""
数据条目的起始索引
# x -> items_per_page*(x-1) ~ items_per_page*x
:return:
"""
return self.per_page * (self.current_page - 1) @property
def end(self):
"""
数据条目的结束索引
# x -> items_per_page*(x-1) ~ items_per_page*x
:return:
"""
return self.per_page * self.current_page @property
def page_html(self):
# 获取正确的开始页码和结束页码
# 判断真实的页码数是否超过 per_page_count
if self.pager_count > self.per_page_count:
# 如果当前页 < half_per_page_count
if self.current_page <= self.half_per_page_count:
pager_start = 1
pager_end = self.per_page_count
# 如果当前页码 大于 half_per_page_count 并且 小于 pager_count - half_per_page_count
elif self.current_page <= self.pager_count - self.half_per_page_count:
pager_start = self.current_page - self.half_per_page_count
pager_end = self.current_page + self.half_per_page_count
# 如果当前页码大于 pager_count - half_per_page_count
else:
pager_start = self.pager_count - self.per_page_count + 1
pager_end = self.pager_count
else:
pager_start = 1
pager_end = self.pager_count page_list = []
first_page = '<a href="{}?page=1">首页</a>'.format(self.base_url)
page_list.append(first_page)
if self.current_page > 1:
prev = '<a href="{}?page={}">上一页</a>'.format(self.base_url,self.current_page - 1)
else:
prev = '<a href="javascript:void(0)" disabled="true">上一页</a>'
page_list.append(prev) # 循环生成HTML
for i in range(pager_start, pager_end + 1):
if i == self.current_page:
tpl = '<a class="active" href="{}?page={}">{}</a>'.format(self.base_url,i, i)
else:
tpl = '<a href="{}?page={}">{}</a>'.format(self.base_url,i, i)
page_list.append(tpl) if self.current_page < self.pager_count:
nex = '<a href="{}?page={}">下一页</a>'.format(self.base_url,self.current_page + 1)
else:
nex = '<a href="javascript:void(0)" disabled="true">下一页</a>'.format(self.current_page + 1)
page_list.append(nex) last_page = '<a href="{}?page={}">尾页</a>'.format(self.base_url,self.pager_count)
page_list.append(last_page)
page_str = "".join(page_list)
return page_str

  

# 视图函数

from django.shortcuts import render,HttpResponse

# Create your views here.

from mypaginator import models
from utils.paginator import Paginator def hosts(request):
"""创建测试数据"""
# for i in range(303):
# name = "c%s.com" % i
# port = i
# models.Host.objects.create(hostname=name,port=port)
# return HttpResponse("OK") # 后端生成好页面HTML,然后返回给前端显示
_page_str = """
<a href="hosts.html?page=1">1</a>
<a href="hosts.html?page=2">2</a>
<a href="hosts.html?page=3">3</a>
"""
# 前端请求的url:http://127.0.0.1:8001/hosts.html?page=2
current_page = int(request.GET.get('page'))
# 首先获得数据总条数
all_count = models.Host.objects.all().count()
pager = Paginator(all_count=all_count,current_page=current_page,base_url=request.path_info)
# 通过切片的形式从数据库中拿到对应页数的items
host_list = models.Host.objects.all()[pager.start:pager.end]
page_str = pager.page_html
return render(request,'hosts.html',locals())

  

# 前端HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.mypagination a{
display: inline-block;
padding: 5px 8px;
background-color: darkslateblue;
margin: 5px;
color: white;
}
.mypagination a.active{
background-color: green;
}
</style>
</head>
<body>
<h1>主机列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>主机名</th>
<th>端口</th>
</tr>
</thead>
<tbody>
{% for row in host_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.hostname }}</td>
<td>{{ row.port }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="mypagination">
{{ page_str|safe }}
</div>
</body>
</html>

  

Python自定义分页组件的更多相关文章

  1. 基于 Python 的自定义分页组件

    基于 Python 的自定义分页组件 分页是网页中经常用到的地方,所以将分页功能分出来,作为一个组件可以方便地使用. 分页实际上就是不同的 url ,通过这些 url 获取不同的数据. 业务逻辑简介 ...

  2. Django框架---- 自定义分页组件

    分页的实现与使用 class Pagination(object): """ 自定义分页 """ def __init__(self,cur ...

  3. Angular4.+ ngx-bootstrap Pagination 自定义分页组件

    Angular4 随笔(二)  ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...

  4. vue 自定义分页组件

    vue2.5自定义分页组件,可设置每页显示条数,带跳转框直接跳转到相应页面 Pagination.vue 效果如下图: all: small(只显示数字和上一页和下一页): html <temp ...

  5. python web 分页组件

    闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...

  6. vue自定义分页组件---切图网

    vue2.5自定义分页组件 Pagination.vue,可设置每页显示条数,带跳转框直接跳转到相应页面,亲测有用.目前很多框架自带有分页组件比如elementUI,不过在面对一个拿到PSD稿,然后重 ...

  7. angular自定义分页组件(实用)

    功能描述:分页,点击按钮或者下一页获取分页接口,同时active到对应页码. html模块: <page page-count="totalPage" on-click-pa ...

  8. Flex4 自定义分页组件

    自己写的Flex4分页组件,去伪存真,只实现基本的分页功能,数据过滤神马的都不应该是分页组件干的活,有呆毛才有真相: [源代码下载] Flex自从转手给Apache后人气急跌,本人也很捉鸡,尽管Apa ...

  9. jquery ajax自定义分页组件(jquery.loehpagerv1.0)原创

    简单的两个步骤截可调用 <script src="<%=basePath%>/resources/js/jquery-1.7.1.min.js"></ ...

随机推荐

  1. Hdoj 2050.折线分割平面 题解

    Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面 ...

  2. 自学Python4.4-装饰器的进阶

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  3. 【转】C++命名空间 namespace的作用和使用解析

    一. 为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中 常见的同名冲突. 在 C语言中定义了3个层次的作用域,即文件(编译单元).函数和复合语句.C ...

  4. proxy.conf编写

    #这里的test.com要与proxy_pass http://test.com 一至!upstream test.com { ip_hash; server 172.16.0.20:80; serv ...

  5. pandas to_sql

    实例: import pymysql import pandas as pd import numpy as np from sqlalchemy import create_engine df = ...

  6. 蓝桥杯 错误票据 (stringstream的使用)

    题目链接:http://lx.lanqiao.cn/problem.page?gpid=T28 问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是 ...

  7. iView 的分页结合表格用法

    HTML: <Table border stripe ref="selection" :columns="columns" :data="now ...

  8. Day9--Python--函数入门

    函数神马是函数: 函数是对功能或动作的封装函数的定义: def 函数名(形参列表): #参数 函数体(return) 调用: ret = 函数名(实参列表) 函数名就是变量名: 函数名的命名规则:变量 ...

  9. hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)

    传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...

  10. bzoj2938 AC自动机 + 拓扑排序找环

    https://www.lydsy.com/JudgeOnline/problem.php?id=2938 题意:给出N个01病毒序列,询问是否存在一个无限长的串不存在病毒序列 正常来说,想要寻找一个 ...