前端页面:
templatetag
from django import template
from django.utils.html import format_html
register = template.Library()
@register.simple_tag
def limit_page(current_page, pg):
scope = abs(current_page - pg)
if scope < 3:
if current_page == pg:
page_re = """
aria-controls="dataTables-example" tabindex="0" id="dataTables-example_previous">
%s
""" % pgelse:
page_re = """
aria-controls="dataTables-example" tabindex="0" id="dataTables-example_previous">
""" % (pg, pg)return format_html(page_re)
else:
return ""
views:
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
def page_t(request):
page = request.GET.get("page")
tdata = models.Tpage.objects.all()
paginator = Paginator(tdata, 2) # 每页多少条数据
try:
tdata = paginator.page(page) # 返回 page(页数)的数据
except PageNotAnInteger:
tdata = paginator.page(1) # 第一页
except EmptyPage:
tdata = paginator.page(paginator.num_pages) # 最后一页
return render(request, "table.html", {"tdata": tdata})