|
41、python_django_web开发_课程Courses列表页面的分页与排序_笔记
课程列表页面 公开课列表页面
- from django.views.generic import View
- from .models import Course
- from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
- class CourseListView(View):
- def get(self, request):
- all_courses = Course.objects.all().order_by('-add_time')
- hot_courses = Course.objects.all().order_by('-click_num')[:3]
- # 对点击数 学生人数的排序
- sort = request.GET.get('sort', '')
- if sort:
- if sort == 'students':
- all_courses = all_courses.order_by('-student_num')
- elif sort == 'hot':
- all_courses = all_courses.order_by('-click_num')
- # 课程列表页面的分页功能
- try:
- page = request.GET.get('page', 1)
- except PageNotAnInteger:
- page = 1
- p = Paginator(all_courses, 2, request=request)
- courses = p.page(page)
- return render(request, 'course-list.html', {
- 'all_courses': courses,
- 'sort': sort,
- 'hot_courses': hot_courses,
- })
复制代码
|
|