1、复制teracher-list.html到templates目录下
2、编辑teacher-list.html,继承base模板
3、编辑organization.views.py
... from .models import Teacher class TeacherListView(View): def get(self, request): all_teachers = Teacher.objects.all() return render(request, ‘teachers-list.html‘, { ‘all_teachers‘: all_teachers, })
4、配置url,编辑organization.urls.py
... from .views import TeacherListView urlpatterns = [ ... url(r‘teacher/list/$‘, TeacherListView.as_view(), name=‘teacher_list‘ ), ]
5、修改index.html,base.html中的授课教师链接
6、先把所有的讲师都展示出来
分页
编辑organization.views.py
class TeacherListView(View): def get(self, request): all_teachers = Teacher.objects.all() # 分页 try: page = request.GET.get(‘page‘, 1) except PageNotAnInteger: page = 1 p = Paginator(all_teachers, 2, request=request) teachers = p.page(page) return render(request, ‘teachers-list.html‘, { ‘all_teachers‘: teachers, })
编辑前端页码
修改前面的展示教师列表
人气排序 和统计教师
编辑organization.views.py
class TeacherListView(View): def get(self, request): all_teachers = Teacher.objects.all() # 人气排序 sort = request.GET.get(‘sort‘, ‘‘) if sort: if sort == ‘hot‘: all_teachers = all_teachers.order_by(‘-click_nums‘) # 统计教师数 teacher_nums = all_teachers.count() # 分页 try: page = request.GET.get(‘page‘, 1) except PageNotAnInteger: page = 1 p = Paginator(all_teachers, 2, request=request) teachers = p.page(page) return render(request, ‘teachers-list.html‘, { ‘all_teachers‘: teachers, ‘sort‘: sort, ‘teracher_nums‘: teacher_nums })
前端配置
讲师排行榜
编辑organization.views.py
class TeacherListView(View): def get(self, request): all_teachers = Teacher.objects.all() # 人气排序 sort = request.GET.get(‘sort‘, ‘‘) if sort: if sort == ‘hot‘: all_teachers = all_teachers.order_by(‘-click_nums‘) # 统计教师数 teacher_nums = all_teachers.count() # 讲师排行榜 rank_teachers = all_teachers.order_by(‘-click_nums‘)[:2] # 分页 try: page = request.GET.get(‘page‘, 1) except PageNotAnInteger: page = 1 p = Paginator(all_teachers, 2, request=request) teachers = p.page(page) return render(request, ‘teachers-list.html‘, { ‘all_teachers‘: teachers, ‘sort‘: sort, ‘teracher_nums‘: teacher_nums, ‘rank_teachers‘: rank_teachers, })
前端展示