24Django实战第24天:讲师列表页

Posted 何波安的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了24Django实战第24天:讲师列表页相关的知识,希望对你有一定的参考价值。

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,
        })

前端展示

 

以上是关于24Django实战第24天:讲师列表页的主要内容,如果未能解决你的问题,请参考以下文章

17Django实战第17天:机构详情展示

VUE项目实战24分页效果实现

26Django实战第26天:配置全局导航

100天精通Oracle-实战系列(第24天)Oracle 数据泵表导出导入

100天精通Oracle-实战系列(第24天)Oracle 数据泵表导出导入

100天精通Oracle-实战系列(第24天)Oracle 数据泵表导出导入