1 .在应用下创建tamplatetgs文件
2 .再在tamplatetags文件下创建pagetag.py
pagetag.py
from django import template
register = template.Library()
from django.utils.html import format_html
# 自定义分页标签
@register.simple_tag
def showpage(count,request):
'''
count 总页数
request 请求对象
'''
# 接受当前的页码数
p = int(request.GET.get('page',1))
# 获取当前请求中所有参数
data = request.GET.dict()
args = ''
for k,v in data.items():
print(k,v)
if k != 'page':
args += '&'+k+'='+v
# {'types': 'all', 'keywords': 'wu','page':2}
# &types=all&keywords=wu
# types=allkeywords=wu
# &types=all&keywords=wu
start = p-5
end = p+4
# 判断 如果当前页 小于 5
if p <= 5:
start = 1
end = 10
# 判断 如果当前页 大于 总页数-5
if p > count-5:
start = count-9
end = count
# 判断 如果总页数 小于 10
if count < 10:
start = 1
end = count
pagehtml = ''
# 首页 #这里加?page=1{args}解决了换页搜索不会失效
pagehtml += '<li><a href="?page=1{args}">首页</a></li>'.format(args=args)
# 上一页
if p > 1:
pagehtml += '<li><a href="?page={p}{args}">上一页</a></li>'.format(p=p-1,args=args)
for x in range(start,end+1):
# 判断是否为当前页
if p == x:
pagehtml += '<li class="am-active"><a href="?page={p}{args}">{p}</a></li>'.format(p=x,args=args)
else:
pagehtml += '<li><a href="?page={p}{args}">{p}</a></li>'.format(p=x,args=args)
# 下一页
if p < count: #这里加?page={p}{args}解决了换页搜索不会失效
pagehtml += '<li><a href="?page={p}{args}">下一页</a></li>'.format(p=p+1,args=args)
# 尾页
pagehtml += '<li><a href="?page={p}{args}">尾页</a></li>'.format(p=count,args=args)
# 如果不用format_html()返回 ,html将会被自动转义为字符串
return format_html(pagehtml)
index.html
搜索
<form>
<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
<div class="am-form-group tpl-table-list-select">
<select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">
<option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>全局搜索</option>
<option value="phone" {% if request.GET.types == 'phone' %} selected {% endif %}>手机号</option>
<option value="id" {% if request.GET.types == 'id' %} selected {% endif %}>ID</option>
<option value="nikename" {% if request.GET.types == 'nikename' %} selected {% endif %}>昵称</option>
<option value="email" {% if request.GET.types == 'email' %} selected {% endif %}>邮箱</option>
</select>
</div>
</div>
<div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
<div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
<!-- keywords 是搜索关键字 -->
<input type="text" name="keywords" value="{{ request.GET.keywords }}" class="am-form-field ">
<span class="am-input-group-btn">
<button class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
</span>
</div>
</div>
</form>
分页
<ul class="am-pagination tpl-pagination"> <!-- 装饰模块由u或py文件传过来的li完成 -->
<!-- <li class="am-disabled"><a href="#">«</a></li> -->
<!-- <li class="am-active"><a href="#">1</a></li> -->
<!-- 这里引入了 pagetag模块 -->
{% load pagetag %}
<!-- showpage 是pagetag模块的函数 通过reqest中间件请求过来 -->
<!-- userlist.paginator.num_pages 是通过request中间件从views获取 -->
{% showpage userlist.paginator.num_pages request %}
<!-- <li><a href="#">»</a></li> -->
</ul>
from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.contrib.auth.hashers import make_password, check_password
from .. models import Users
from web.settings import BASE_DIR
import os
# 会员列表
def user_index(request):
# 获取所有用户数据
data = Users.objects.all()
# 获取搜索条件/比如: 全局搜索,手机号搜索,人名,
types = request.GET.get('types',None)
#获取搜索的关键字
keywords = request.GET.get('keywords',None)
# 判断是否搜索
if types == 'all':
from django.db.models import Q
data = data.filter(Q(id__contains=keywords)|Q(nikename__contains=keywords)|Q(phone__contains=keywords)|Q(email__contains=keywords))
elif types :
search = {types+'__contains':keywords}
data = data.filter(**search)
# 导入分页类
from django.core.paginator import Paginator
# 实例化分页类
p = Paginator(data, 10)
# 获取当前的页码数
pageindex = request.GET.get('page',1)
# 获取当前页的数据
userlist = p.page(pageindex)
# 获取所有的页码
# pages = p.page_range #这是获取一个可迭代对象
# pages = p.num_pages
# 分配数据
context = {'userlist':userlist}
# 加载模板
return render(request,'myadmin/users/index.html',context)