(编辑:jimmy 日期: 2024/12/27 浏览:2)
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})