码迷,mamicode.com
首页 > 其他好文 > 详细

django分页模板

时间:2019-01-19 16:26:25      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:nav   append   +=   list   函数   label   else   djang   next   

视图函数:

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">&laquo;</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">&raquo;</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分页模板

标签:nav   append   +=   list   函数   label   else   djang   next   

原文地址:https://www.cnblogs.com/sw-z/p/10291707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!