分页
-
pager.py
class Pagination(object): def __init__(self,total_count,current_page,per_page_item_num=20,max_page_num=7): #数据总条目 self.total_count = total_count #当前页 try: v = int(current_page) if v <= 0: v = 1 self.current_page = v except Exception as e: self.current_page = 1 #每页显示条目 self.per_page_item_num = per_page_item_num #每页最多显示页码 self.max_page_num = max_page_num def start(self): return (self.current_page-1)*self.per_page_item_num def end(self): return self.current_page*self.per_page_item_num @property def num_pages(self): # 总页数 a,b = divmod(self.total_count,self.per_page_item_num) if b == 0: return a return a+1 def page_num_range(self): #如果总页数小于每页最多显示页码数量,就显示1-总页码 if self.num_pages < self.max_page_num: return range(1,self.num_pages+1) #如果总页数有很多 part = int(self.max_page_num/2) if self.current_page <= part: return range(1,self.max_page_num+1) #最后一页只显示最后10个页码即可 if (self.current_page+part) > self.num_pages: return range(self.num_pages-self.max_page_num+1,self.num_pages+1) return range(self.current_page-part,self.current_page+part+1) def page_str(self,base_url): page_list = [] first_page = '<li><a href="%s?p=1">首页</a></li>'%base_url page_list.append(first_page) if self.current_page == 1: prev_page = '<li><a href="javascript:void(0);" class="hide">上一页</a>' else: prev_page = '<li><a href="%s?p=%s">上一页</a></li>'%(base_url,self.current_page-1) page_list.append(prev_page) for i in self.page_num_range(): if i == self.current_page: temp = '<li class="active"><a href="%s?p=%s">%s</a></li>' % (base_url,i, i) else: temp = '<li><a href="%s?p=%s">%s</a></li>'%(base_url,i,i) page_list.append(temp) if self.current_page == self.num_pages: next_page = '<li><a href="javascript:void(0);" class="hide">下一页</a></li>' else: next_page = '<li><a href="%s?p=%s">下一页</a></li>' % (base_url,self.current_page + 1) page_list.append(next_page) last_page = '<li><a href="%s?p=%s">尾页</a></li>'%(base_url,self.num_pages) page_list.append(last_page) return ''.join(page_list)
-
views.py
from utils.pager import Pagination from django.urls import reverse def index(request,*args,**kwargs): ''' 博客网站主页 :param kwargs: {article_type_id:1} :return: index.html ''' article_type_list = Classification.objects.values('id','name') if kwargs: classification_id = int(kwargs['classification_id']) #reverse通过别名反向生成URL base_url = reverse('index',kwargs=kwargs)#all/1.html else: classification_id = None base_url = '/' #/ data_count = Article.objects.filter(**kwargs).count() article_list = Article.objects.filter(**kwargs).order_by('-id')#文章对象列表倒序排列,最新的在最前面 page_obj = Pagination(data_count,request.GET.get('p')) data = article_list[page_obj.start():page_obj.end()] page_str = page_obj.page_str(base_url) return render( request, 'index.html', { 'article_type_list':article_type_list, 'classification_id':classification_id, 'data':data, 'page_str':page_str, } )
-
index.html
<div class="page-area"> <ul class="pagination"> {{ page_str|safe }} </ul> </div>