Django 實現web分頁顯示

2021-09-08 11:49:27 字數 1387 閱讀 1241

從資料庫取出資料,根據事先定義的每頁顯示的數量,進行分頁計算,得到頁數,構造html,返回給前端解析顯示。

大致分為兩種:

def page_student_list(request,page):

# page = int(page)

# start_num = (page-1)*10

# end_num = page*10

# db_student = student.objects.all()

## student_count = db_student.count() #返回查詢的條數

## #判斷頁碼的範圍

# pageend = student_count/10

# if pageend != int(pageend):

# pageend += 1

## if page < 3:

# page_start = 1

# page_end = 5

# # page_range = range(1, 6)

# else:

# page_start = page-2

# page_end = page+3

## if page_end >= pageend: #分頁腳部

# page_end = pageend##

# page_range = range(page_start, int(page_end)+1)

# #page_range = range(1,int(page_end)+1 ) #獲取分頁列表##

# students = db_student[start_num:end_num]

# if not students:

# students = db_student[0:10]

# return render_to_response("studentlist.html", locals())

第二種:

def page_student_list(request,page):

page=int(page)      #當前頁碼

db_student = student.objects.all()      #   獲取資料庫資訊

p = paginator(db_student,10)    #10條資訊一頁

count = int((p.count)/10)   

詳細的內容請檢視:

缺點:django1.6版本之前無法使用

Django的分頁顯示實現

django中提供了乙個類paginator專門用來管理和處理分頁資料,所以我們在使用之前先導入好相應的類,另外這裡我們也匯入了待會會用到的處理異常的兩個類emptypage和pagenotaninteger from django.core.paginator import paginator,e...

django 實現分頁功能

分頁效果 檢視 1 coding utf 8 2 from django.shortcuts import render,get object or 404 3 from django.core.paginator import paginator,pagenotaninteger,emptypag...

輕鬆實現Django分頁

pip install djangorestframework安裝csrf第三方庫之後,我們就可以使用自帶分頁器paginator來進行內容的分頁 如果取不到分頁物件時,設定頁碼為1 page int request.get.get page 1 取分頁物件,orders為需要分頁的物件,一般為mo...