话不多说,先上效果图: 热乎乎的代码紧随其后,思路可能比较割裂,有更优雅的写法,小伙伴们多多建言哈。
后端python:
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
def datalists(request):
request.encoding='utf-8'
data_list=Data_List.objects.all()
to_page=topage(data_lilst,1)
to_range=torange(to_page,1)
response={}
response['data_list']=to_page
response['range_list']=to_range
return render(request,'data_show.html',response)
def topage(table_list,page):
paginator = Paginator(table_list,2)
try:
result = paginator.page(page)
except PageNotAnInteger:
result = paginator.page(1)
except InvalidPage:
result = paginator.page(1)
except EmptyPage:
result = paginator.page(paginator.num_pages)
return result
def torange(topage,page):
if topage.paginator.num_pages<=10:
result=range(1,topage.paginator.num_pages+1)
else:
if page<=5:
result=range(1,11)
elif page>=topage.paginator.num_pages-4:
result=range(topage.paginator.num_pages-9,topage.paginator.num_pages+1)
else:
result=range(page-5,page+5)
return result
前端html:
<tr>
<td colspan="12">
<div class="pagination-info pull-left">共{{data_list.paginator.count}}条记录,当前第{{data_list.number}}/{{data_list.paginator.num_pages}}页,每页20条记录</div>
<div class="pull-right king-page-box">
<ul class="pagination pagination-small pull-right">
{% if data_list.has_previous %}
<li id="last"><a>?</a></li>
{% endif %}
{% for item in range_list %}
{% if data_list.number == item %}
<li class="active page" ><a>{{ item }}</a></li>
{% else %}
<li><a class="page">{{ item }}</a></li>
{% endif %}
{% endfor %}
{% if data_list.has_next %}
<li id="next"><a >?</a></li>
{% endif %}
</ul>
</div>
</td>
</tr>
关于前端部分就不再赘述,愿所有开发人员的目的地都是星辰大海。
|