自定义分页
view视图函数:
def index_page(request,page): #分页视图
display_linkpage_count = 5 #页面下方展示的页面选项数量
display_item = 10 #每页显示数据条目
count = Host.objects.all().count() #数据库中总条数
all_page, current_page, start_item, end_item = postion(count, display_item, page)
#调用postion函数处理
result = Host.objects.all()[start_item:end_item] #获取当前页要展示的数据
current_count= Host.objects.all()[start_item:end_item].count() #当前页显示数据条目
page_html = pager(current_page, all_page,display_linkpage_count) #调用自定义分页函数
ret = {'data':result,'count':count,'current_count':current_count,'page_html':page_html}
return render(request,'ajax_app/index.html',ret)
路由:
url(r'^index_page/(?P<page>\d*)', views.index_page, name='index_page')
两个自定义的函数:
from django.utils.safestring import mark_safe #把字符串安全输出到前端
def postion(count,display_item,page):
all_page = int((count + display_item - 1) / display_item) # 总页数,向上取整数,如5/2为3
try:
current_page = int(page)
if current_page <= 0: # 如果为负数,则默认为1
current_page = 1
elif current_page > all_page:
current_page = all_page
except Exception, e:
current_page = 1 # 当前页ID,默认为1
finally:
pass
# Host.objects.all()获取到的数据返回为列表,从0开始
# 0--9 Host.objects.all()[0:10] 前10个数据
# 10--19 Host.objects.all()[10:20]
# 20--29 Host.objects.all()[20:30]
start_item = (current_page - 1) * display_item
end_item = current_page * display_item
return all_page,current_page,start_item,end_item
def pager(current_page,all_page,display_linkpage_count): #自定义分页
#current_page为当前页,all_page为总页数,display_linkpage_count为下方要显示的页数链接
temp_html = []
first_html = "<a href='/ajax_app/index_page/%d'>首页</a>" % (1)
temp_html.append(first_html)
if current_page <=1:
prev_html = "<a href='#'>上一页</a>"
else:
prev_html = "<a href='/ajax_app/index_page/%d'>上一页</a>" % (current_page - 1)
temp_html.append(prev_html)
begin = current_page - (display_linkpage_count / 2)
end = current_page + (display_linkpage_count / 2)
if all_page <= display_linkpage_count: #当总页数小于下方要显示的页数链接
begin = 1
end = all_page
else:
if begin == 0:
end = current_page + (current_page - begin + 1)
begin = 1
elif begin < 0:
end = current_page + (current_page - begin + 1)+1
begin = 1
elif end > all_page:
begin = current_page - (end - current_page + 1)
end = all_page
for i in range(begin,end+1):
if current_page == i:
a_html = "<a href='/ajax_app/index_page/%d' style='color: chartreuse'>%d</a>" % (i,i) #如果是当前页,则显示绿色
else:
a_html = "<a href='/ajax_app/index_page/%d'>%d</a>" % (i, i)
temp_html.append(a_html)
if current_page >= all_page:
next_html = "<a href='#'>下一页</a>"
else:
next_html = "<a href='/ajax_app/index_page/%d'>下一页</a>" % (current_page+1)
temp_html.append(next_html)
end_html = "<a href='/ajax_app/index_page/%d'>尾页</a>" % (all_page)
temp_html.append(end_html)
temp_str = mark_safe('-'.join(temp_html)) # 使用mark_safe方法静态输出字符串,使用join方法将列表连接为字符串
return temp_str
html页面:
<body>
<h1>主机信息</h1>
<table border="1">
<tr>
<td>主机名</td>
<td>IP地址</td>
</tr>
{% for item in data %}
<tr>
<td>{{ item.hostname }}</td>
<td>{{ item.IP }}</td>
</tr>
{% endfor %}
</table>
<div>
<td>{{ page_html }}</td>
</div>
<div>当前页条数:{{ current_count }}</div>
<div>总条数:{{ count }}</div>
</body>