码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-25 12:09:56      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:rom   讲师   图片   前端   cts   gpo   技术分享   nbsp   hot   

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

前端展示

技术分享图片

 

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

标签:rom   讲师   图片   前端   cts   gpo   技术分享   nbsp   hot   

原文地址:https://www.cnblogs.com/sellsa/p/8643160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!