东方耀AI技术分享
标题:
41、python_django_web开发_课程Courses列表页面的分页与排序_笔记
[打印本页]
作者:
东方耀
时间:
2017-11-28 08:09
标题:
41、python_django_web开发_课程Courses列表页面的分页与排序_笔记
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,
})
复制代码
作者:
xiaoma
时间:
2017-12-14 14:52
1
作者:
xiaodan
时间:
2018-12-26 15:46
谢谢老师
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4