django 實現分頁功能

2021-08-23 14:26:01 字數 1298 閱讀 7394

分頁效果:

檢視**:

1 # -*- coding: utf-8 -*-

2 from django.shortcuts import render,get_object_or_404

3 from django.core.paginator import paginator,pagenotaninteger,emptypage

4 5 from .models import article

6 7 # create your views here.

8 9 def index(request):

10 # latest_article_list = article.objects.order_by('update')[:5]

11 # context =

12 # return render(request, 'blog/index.html',context)

13 article_list = article.objects.all().order_by('cre_date')

14 paginator = paginator(article_list,2) #show 2 articles per page

15 16 page = request.get.get('page')

17 18 try:

19 articles = paginator.page(page)

20 except pagenotaninteger:

21 #頁碼不是整數,返回第一頁。

22 articles = paginator.page(1)

23 except emptypage:

24 articles = paginator.page(paginator.num_pages)

25 26 return render(request, 'blog/index.html', )

paginator是分頁例項,page是鏈結傳遞到後端的頁碼引數,articles是每頁的例項。

paginator和articles的屬性和方法詳見文件:

前端**:

1 

2 3 4

37

38 39

django 分頁功能

django 分頁功能 django自帶了paginator,功能有一定侷限,使用django pure pagination功能更為強大 github傳送門 1 使用pip源安裝 pip install django pure pagination pure pagination 3 在setti...

django分頁功能

採用django自帶的paginator功能 from django.core.paginator import paginator food foodinfo.objects.filter food name contains searchstr pag paginator food,25 每頁顯...

使用Django實現分頁器功能

要使用django實現分頁器,必須從django中匯入paginator模組 from django.core.paginator import paginator假如現在有150條記錄要顯示,每頁顯示10條 from django.core.paginator import paginator 匯...