东方耀AI技术分享

标题: 41、python_django_web开发_课程Courses列表页面的分页与排序_笔记 [打印本页]

作者: 东方耀    时间: 2017-11-28 08:09
标题: 41、python_django_web开发_课程Courses列表页面的分页与排序_笔记


41、python_django_web开发_课程Courses列表页面的分页与排序_笔记
课程列表页面  公开课列表页面

  1. from django.views.generic import View

  2. from .models import Course

  3. from pure_pagination import Paginator, EmptyPage, PageNotAnInteger


  4. class CourseListView(View):
  5.     def get(self, request):
  6.         all_courses = Course.objects.all().order_by('-add_time')

  7.         hot_courses = Course.objects.all().order_by('-click_num')[:3]

  8.         # 对点击数 学生人数的排序
  9.         sort = request.GET.get('sort', '')
  10.         if sort:
  11.             if sort == 'students':
  12.                 all_courses = all_courses.order_by('-student_num')
  13.             elif sort == 'hot':
  14.                 all_courses = all_courses.order_by('-click_num')

  15.         # 课程列表页面的分页功能
  16.         try:
  17.             page = request.GET.get('page', 1)
  18.         except PageNotAnInteger:
  19.             page = 1

  20.         p = Paginator(all_courses, 2, request=request)

  21.         courses = p.page(page)

  22.         return render(request, 'course-list.html', {
  23.             'all_courses': courses,
  24.             'sort': sort,
  25.             'hot_courses': hot_courses,
  26.         })
复制代码



作者: xiaoma    时间: 2017-12-14 14:52
1
作者: xiaodan    时间: 2018-12-26 15:46
谢谢老师




欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4