django分页模板
Posted sw-z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django分页模板相关的知识,希望对你有一定的参考价值。
视图函数:
pages = request.GET.get(‘page‘,‘1‘)
if pages.isdigit() == False:
pages = ‘1‘
pages = int(pages)
PERPAGE = 10
MAXPAGE = 11
half_max_page = MAXPAGE // 2
total_data = models.__.objects.all().count()
total_page, count = divmod(total_data, PERPAGE)
if total_data == 0:
total_page = 1
if count:
total_page += 1
page_start = pages - half_max_page
page_end = pages + half_max_page
if page_start <= 1:
page_start = 1
if total_page < MAXPAGE:
page_end = total_page
else:
page_end = MAXPAGE
elif page_end >= total_page:
page_end = total_page
page_start = page_end - (MAXPAGE - 1)
res = models.__.objects.all().order_by()[(pages - 1) * 10:pages * 10]
up_page = pages - 1
if up_page < 1:
up_page = 1
html_list = []
if pages > 1:
html_list.append(
‘<li><a href="/__/?page={0}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘.format(
up_page))
html_list.append(‘<li><a href="/publisher/?page=1">首页</a></li>‘)
for i in range(page_start, page_end + 1):
if i == pages:
html = ‘<li><a class="page-active" href="/__/?page={0}">{0}</a></li>‘.format(i)
else:
html = ‘<li><a href="/__/?page={0}">{0}</a></li>‘.format(i)
html_list.append(html)
down_page = pages + 1
if pages < total_page:
html_list.append(‘<li><a href="/__/?page={0}">尾页</a></li>‘.format(total_page))
html_list.append(
‘<li><a href="/__/?page={0}" aria-label="Next"><span aria-hidden="true">»</span></a></li>‘.format(
down_page))
page_html = ‘‘.join(html_list)
模板代码:
<nav aria-label="Page navigation">
<ul class="pagination">
{{ page_html|safe }}
</ul>
</nav>
以上是关于django分页模板的主要内容,如果未能解决你的问题,请参考以下文章