利用django 框架自带的分页工具 Paginator
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def get_page_list(datas, page_no=1, page_size=12):
"""
分页数据
:param datas
:return:
"""
result = {}
if page_no and page_size:
paginator = Paginator(datas, page_size)
try:
datas = paginator.page(page_no)
except PageNotAnInteger:
datas = paginator.page(1)
except EmptyPage:
datas = paginator.page(paginator.num_pages)
page = dict()
page['total'] = paginator.count
page['page_count'] = paginator.num_pages
page['page_no'] = int(page_no)
page['page_rows'] = range(paginator.num_pages)
result['page'] = page
result['datas'] = datas
return result
通用页面:
{% load i18n %}
<div id="js_turn_page" class="dataPage_cn tr">
{% if page.page_no > 1 %}
<a data-page="{{ page.page_no|add:'-1' }}" href="javascript:;" class="turn-page dataPage_prev" direction="prev"><{% trans 'Revios' %}</a>
{% endif %}
{% for i in page.page_rows %}
{% if i|add:'1' == 1 or i|add:'1' == page.page_count %}
<a data-page="{{ i|add:'1' }}" href="javascript:;" class="turn-page dataPage_number {% ifequal page.page_no i|add:'1' %}dataPage_numberFocus{% endifequal %}">{{ i|add:'1' }}</a>
{% elif page.page_no|add:'4' == i or page.page_no|add:'-6' == i %}
...
{% elif page.page_no|add:'4' < i or page.page_no|add:'-6' > i %}
{% elif i|add:'1' == page.page_no %}
<a data-page="{{ i|add:'1' }}" href="javascript:;" class="turn-page dataPage_number {% ifequal page.page_no i|add:'1' %}dataPage_numberFocus{% endifequal %}">{{ i|add:'1' }}</a>
{% else %}
<a data-page="{{ i|add:'1' }}" href="javascript:;" class="turn-page dataPage_number {% ifequal page.page_no i|add:'1' %}dataPage_numberFocus{% endifequal %}">{{ i|add:'1' }}</a>
{% endif %}
{% endfor %}
{% ifnotequal page.page_no page.page_count %}
<a data-page="{{ page.page_no|add:'1' }}" href="javascript:;" class="turn-page dataPage_next" direction="next">{% trans 'Next' %}></a>
{% endifnotequal %}
{% if jump %}
<span class="dataPage_goTo">{% trans 'Go to Page' %}:<input type="text" /><input type="button" value="{% trans 'GO' %}" /></span>
{% endif %}
</div>
翻页 js:
$('a.turn-page').click(function () {
var page_no = $(this).data('page');
$('#page_no').val(page_no);
var url_param = $('#param_form').serializeObject();
window.location = genUrl(window.location.href.split('?')[0], url_param);
});
function genUrl(url, params) {
var new_params = {}
for (var key in params) {
if (params[key]) {
new_params[key] = params[key]
}
}
return url + '?' + $.param(new_params);
}