7.django之自定义分页记录
只是大概记录下步骤:
1.表结构:
class UserProfile(models.Model):
'''
用户表
'''
user = models.OneToOneField(User,verbose_name='后台用户')
#名字
name = models.CharField(max_length=32,verbose_name='姓名')
#头像
head_img = models.ImageField(blank=True,null=True,upload_to="uploads/portrait",verbose_name='头像')
#邮箱
email = models.EmailField(max_length=64,blank=True,null=True,verbose_name='邮箱')
#手机号
phone = models.CharField(verbose_name='手机号',max_length=128)
#部门
department = models.ForeignKey('Group',verbose_name='部门',null=True,blank=True)
#公司ip
ip = models.GenericIPAddressField(max_length=32,verbose_name='办公IP')
#秘钥
secret_key = models.TextField(verbose_name='密钥')
#备注
memo = models.TextField(verbose_name='备注', null=True, blank=True)
#创建时间
create_date = models.DateField(verbose_name='注册时间',auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name = '成员'
verbose_name_plural = '成员'
2.views里函数
#导入分页相关模块
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
def user(request):
group = models.Group.objects.all()
#导入user表
userlist = models.UserProfile.objects.all()
#设置要显示的页面数量
paginator = Paginator(userlist,10)
page = request.GET.get('page')
try:
userlist_objs = paginator.page(page)
print(userlist_objs)
except PageNotAnInteger:
#如果页面不是一个整数,交付第一页。
userlist_objs = paginator.page(1)
except EmptyPage:
userlist_objs = paginator.page(paginator.num_pages)
return render(request,'member/user.html',
{'userlist_objs':userlist_objs,'group':group})
#将被分页模块处理过的导入模板中
3.template里
导入分页处理的自定义标签
{% load memtags %}
用户信息
<tbody>
{% for user in userlist_objs %}
<tr class="gradeX">
<td class="text-center" name="id" value="{{ user.id }}" data-editable='false'>
<input name="id" value="{{ user.id }}" type="checkbox" class="i-checks">
</td>
<td class="text-center hostname"> <a href="{% url 'userdetail' user.id %}">{{ user.name }}</a></td>
<td class="text-center">{{ user.department }}</td>
<td class="text-center">{{ user.ip }}</td>
<td class="text-center">{{ user.phone }}</td>
<td class="text-center" data-editable='false'>
<a value="#" class="conn btn btn-xs btn-warning">分发</a>
<a href="{% url 'userdetail' user.id %}" class="btn btn-xs btn-info">详细资料</a>
</td>
</tr>
{% endfor %}
</tbody>
分页点击
<div class="col-sm-6">
<div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
<ul class="pagination" style="margin-top: 0; float: right">
{% if userlist_objs.has_previous %}
<li class="paginate_button previous disabled" aria-controls="editable" tabindex="0" id="editable_previous">
<a class="?page={{ userlist_objs.previous_page_number }}" href="#">Previous</a>
</li>
{% endif %}
{% for page_num in userlist_objs.paginator.page_range %}
{% gusee_page userlist_objs.number page_num %}
{% endfor %}
{% if userlist_objs.has_next %}
<li class="paginate_button next disabled" aria-controls="editable" tabindex="0" id="editable_next">
<a class="page" href="#">Next</a>
</li>
{% endif %}
</ul>
</div>
</div>
4.自定义标签方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django import template
from django.utils.html import format_html
from django.template.defaultfilters import stringfilter
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
def gusee_page(current_page,loop_num):
offset = abs(current_page - loop_num)
if offset <5:
if current_page == loop_num:
page_ele = ''' <li class="paginate_button active" aria-controls="editable" tabindex="0"><a class="page" href="?page=%s" title="第%s页">%s</a></li>'''%(loop_num,loop_num,loop_num)
else:
page_ele = ''' <li class="paginate_button" aria-controls="editable" tabindex="0"><a class="page" href="?page=%s" title="第%s页">%s</a></li>'''%(loop_num,loop_num,loop_num)
return format_html(page_ele)
else:
return ''