urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^show/', views.show_view, name='show')
]
在views.py中創建show_view函數
from django.http import HttpResponse
from django.shortcuts import render
from .models import *
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.http import JsonResponse
def show_view(request):
# 獲取當前頁碼數
num = request.GET.get('num', 1)
n = int(num)
# 1.查詢stu_student表中的所有數據
stus = Student.objects.all() # 獲取所有的
# django 分頁
pager = Paginator(stus, 2)
# 獲取當前頁面的數據
try:
stuss = Student.objects.all().values()
students = list(stuss)
return JsonResponse({'code': 200, 'data': students})
perpage_data = pager.page(n)
# 返回第一頁的數據
except PageNotAnInteger:
perpage_data = pager.page(1)
# 返回最後一頁的數據
except EmptyPage:
perpage_data = pager.page(pager.num_pages)
return render(request, 'show.html', {'show': stus, 'pager': pager, 'perpage_data': perpage_data})