1 主要代码
views
from utils import pagination
def host(request):
if request.method == "GET":
a1 = models.Host.objects.filter(nid__gt=0)
# a2 = models.Host.objects.filter(nid__gt=0).values('nid','hostname','b_id','b__caption','port')
# a3 = models.Host.objects.filter(nid__gt=0).values_list('nid','hostname','b_id','b__caption','port')
# b_list = models.Business.objects.all()
# return render(request, 'host_page.html', {'a1': a1})
current_page = request.GET.get('p', 1)
current_page = int(current_page)
val = request.COOKIES.get('per_page_count', 10)
val = int(val)
page_obj = pagination.Page(current_page, len(a1), val)
data = a1[page_obj.start:page_obj.end]
page_str = page_obj.page_str("/app1/host/")
return render(request, 'host_page.html', {'a1': data, 'page_str': page_str})
页面
<table border="1">
<thead>
<tr>
<th>主机名</th>
<th>IP</th>
<th>端口</th>
<th>业务线名称</th>
<th>功能</th>
</tr>
</thead>
<tbody>
{% for i in a1 %}
<tr hid="{{ i.nid }}" bid="{{ i.b_id }}">
<td>{{ i.hostname }}</td>
<td>{{ i.ip }}</td>
<td>{{ i.port }}</td>
<td>{{ i.b.caption }}</td>
<td>
<a class="edit_host_1">编辑</a>|
<a class="del_host_1">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
{{ page_str }}
</div>
<script src="/static/jquery.cookie.js"></script>
<script src="/static/jquery-1.12.4.js"></script>
主目录下utils内的pagination文件
__author__ = 'Administrator'
from django.utils.safestring import mark_safe
class Page:
def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):
print("data_count:",data_count)
self.current_page = current_page
self.data_count = data_count
self.per_page_count = per_page_count
self.pager_num = pager_num
@property
def start(self):
return (self.current_page - 1) * self.per_page_count
@property
def end(self):
return self.current_page * self.per_page_count
@property
def total_count(self):
v, y = divmod(self.data_count, self.per_page_count)
if y:
v += 1
return v
def page_str(self, base_url):
page_list = []
if self.total_count < self.pager_num:
start_index = 1
end_index = self.total_count + 1
else:
if self.current_page <= (self.pager_num + 1) / 2:
start_index = 1
end_index = self.pager_num + 1
else:
start_index = self.current_page - (self.pager_num - 1) / 2
end_index = self.current_page + (self.pager_num + 1) / 2
if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
end_index = self.total_count + 1
start_index = self.total_count - self.pager_num + 1
if self.current_page == 1:
prev = '<a class="page" href="javascript:void(0);">上一页</a>'
else:
prev = '<a class="page" href="%s?p=%s">上一页</a>' % (base_url, self.current_page - 1,)
page_list.append(prev)
for i in range(int(start_index), int(end_index)):
if i == self.current_page:
temp = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url, i, i)
else:
temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url, i, i)
page_list.append(temp)
if self.current_page == self.total_count:
nex = '<a class="page" href="javascript:void(0);">下一页</a>'
else:
nex = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1,)
page_list.append(nex)
jump = """
<input type='text' /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
<script>
function jumpTo(ths,base){
var val = ths.previousSibling.value;
location.href = base + val;
}
</script>
""" % (base_url,)
page_list.append(jump)
page_str = mark_safe("".join(page_list))
return page_str
urls里
path('host/', views.host),