美文网首页
Django 开发 MxOnline 项目笔记 -- 第9章 课

Django 开发 MxOnline 项目笔记 -- 第9章 课

作者: 江湖十年 | 来源:发表于2018-03-06 00:00 被阅读68次

    一、课程讲师列表页

    拷贝前端页面 teacher-list.html 和 teacher-detail.html 到 templates/ 目录下


    001.png

    课程讲师列表页


    002.png
    • (1)定义 TeacherListView 类视图,处理课程讲师列表页
    # apps/organization/views.py
    
    class TeacherListView(View):
        """
        课程讲师列表页类视图
        """
        def get(self, request):
            current_page_name = "teacher_list_page"
            all_teachers = Teacher.objects.all().order_by("-add_time")
    
            # 根据人气(点击量)对 all_teachers 进行排序
            sort = request.GET.get("sort", "")
            if sort:
                if sort == "hot":
                    all_teachers = all_teachers.order_by("-click_nums")
    
            # 获取人气排行前3的讲师
            sorted_teachers = all_teachers.order_by("-click_nums")[:3]
    
            # 对课程讲师进行分页
            try:
                page = request.GET.get('page', 1)
            except PageNotAnInteger:
                page = 1
            # Paginator 接收 3 个参数, 需要分页的对象列表, 每页显示的对象数量, 以及 request
            p = Paginator(all_teachers, 2, request=request)
            teachers = p.page(page)
    
            context = {
                "all_teachers": teachers,
                "sorted_teachers": sorted_teachers,
                "sort": sort,
                "current_page_name": current_page_name,
            }
            return render(request, "teachers-list.html", context)
    
    
    • (2)配置 url
    # apps/organization/urls.py
    
    from django.urls import path, re_path
    
    from .views import TeacherListView
    
    
    app_name = "org"
    urlpatterns = [
        ...
        # 课程讲师列表页
        path("teacher/list/", TeacherListView.as_view(), name="teacher_list"),
    ]
    
    
    • (3)前端 teacher-list.html 页面
    # templates/teacher-list.html
    
    {% extends "base.html" %}
    {% load staticfiles %}
    
    {% block title %}课程讲师 - 慕学在线网{% endblock %}
    
    {% block custom_bread %}
        <section>
            <div class="wp">
                <ul  class="crumbs">
                    <li><a href="{% url "index" %}">首页</a>></li>
                    <li>课程讲师</li>
                </ul>
            </div>
        </section>
    {% endblock custom_bread %}
    
    {% block content %}
        <section>
            <div class="wp butler_list_box list">
            <div class='left'>
                <div class="butler_list layout">
                    <div class="head">
                        <ul class="fl tab_header">
                            <li {% if sort == "" %}class="active"{% endif %}><a href="?sort=">全部</a> </li>
                            <li {% if sort == "hot" %}class="active"{% endif %}><a href="?sort=hot">人气 &#8595;</a></li>
                        </ul>
                        <div class="fr butler-num">共<span class="key">9</span>人&nbsp;&nbsp;&nbsp;</div>
                    </div>
                    {% for teacher in all_teachers.object_list %}
                        <dl class="des">
                            <dt>
                                <a href="{% url "org:teacher_detail" teacher.id %}">
                                    <div class="picpic">
                                        <img width="100" height="100" class="scrollLoading" src="{{ MEDIA_URL }}{{ teacher.image }}"/>
                                    </div>
                                </a>
                                <div class="btn">
                                    <div class="fr btn2 bdsharebuttonbox"
                                         data-text="授课教师-奥巴马-慕学在线"
                                         data-desc="我在#慕课网#发现了教师“奥巴马”,对学习中的小伙伴很有帮助,一起来看看吧。"
                                         data-comment="奥巴马金牌讲师,从业年限:5年"
                                         >
                                        <span class="fl">分享</span>
                                        <a href="#" class="bds_more" data-cmd="more"></a>
                                    </div>
                                </div>
                            </dt>
                            <dd>
                                <a href="{% url "org:teacher_detail" teacher.id %}">
                                    <h1>{{ teacher.name }}<span class="key picbig">金牌讲师</span></h1>
                                </a>
                                <ul class="cont">
                                    <li>工作年限:<span>{{ teacher.work_years }}年</span></li>
                                    <li>工作职位:<span>{{ teacher.work_position }}</span></li>
                                    <li>就职公司:<span>{{ teacher.work_company }}&nbsp;</span></li>
                                    <li>年龄:<span>{{ teacher.age }}岁</span></li>
                                    <li>教学特点:<span>{{ teacher.work_points }}</span></li>
                                </ul>
                            </dd>
                            <a class="buy buyservice" href="{% url "org:teacher_detail" teacher.id %}"><br/>查看<br/>详情</a>
                        </dl>
                    {% endfor %}
                </div>
                <div class="pageturn pagination">
                 <ul>
                     {% if all_teachers.has_previous %}
                         <li class="long"><a href="?{{ all_teachers.previous_page_number.querystring }}">上一页</a></li>
                     {% endif %}
                     {% for page in all_teachers.pages %}
                         {% if page %}
                             {% ifequal page all_teachers.number %}
                                 <li class="page active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                             {% else %}
                                 <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li>
                             {% endifequal %}
                         {% else %}
                             <li class="none"><a href="">...</a></li>
                         {% endif %}
                     {% endfor %}
                     {% if all_teachers.has_next %}
                         <li class="long"><a href="?{{ all_teachers.next_page_number.querystring }}">下一页</a></li>
                     {% endif %}
                 </ul>
             </div>
            </div>
            <div class="right layout">
                <div class="head">讲师排行榜</div>
                    {% for hot_teacher in sorted_teachers %}
                        <dl class="des">
                            <span class="num fl">1</span>
                            <a href="/diary/hk_detail/6/">
                                <dt>
                                    <img width="50" height="50" class="scrollLoading"  src="{{ MEDIA_URL }}{{ hot_teacher.image }}"/>
                                </dt>
                            </a>
                            <dd>
                                <a href="/diary/hk_detail/6/">
                                    <h1 title="{{ hot_teacher.name }}">{{ hot_teacher.name }}</h1>
                                </a>
                                <p>工作年限:<span>{{ hot_teacher.work_years }}年</span></p>
                            </dd>
                        </dl>
                    {% endfor %}
                </div>
            </div>
        </section>
    {% endblock content %}
    
    

    二、课程讲师详情页

    003.png
    • (1)定义 TeacherDetailView 类视图
    # apps/organization/views.py
    
    class TeacherDetailView(View):
        def get(self, request, id):
            current_page_name = "teacher_list_page"
            teacher = Teacher.objects.get(pk=id)
            # 获取人气排行前3的讲师
            sorted_teachers = Teacher.objects.all().order_by("-click_nums")[:3]
            # 获取讲师所有课程
            teacher_courses = Course.objects.filter(teacher=teacher)
    
            # 判断用户是否已收藏过这个讲师
            has_teacher_favorite = False
            if UserFavorite.objects.filter(user=request.user, favorite_type=3, favorite_id=teacher.id):
                has_teacher_favorite = True
    
            # 判断用户是否已收藏过这个讲师所在机构
            has_organization_favorite = False
            if UserFavorite.objects.filter(user=request.user, favorite_type=2, favorite_id=teacher.organization.id):
                has_organization_favorite = True
    
            context = {
                "teacher": teacher,
                "sorted_teachers": sorted_teachers,
                "teacher_courses": teacher_courses,
                "has_teacher_favorite": has_teacher_favorite,
                "has_organization_favorite": has_organization_favorite,
                "current_page_name": current_page_name,
            }
            return render(request, "teacher-detail.html", context)
    
    
    • (2)配置 url
    # apps/organization/urls.py
    
    from django.urls import path, re_path
    
    from .views import TeacherDetailView
    
    
    app_name = "org"
    urlpatterns = [
        ...
        # 课程讲师详情页
        path("teacher/detail/<int:id>", TeacherDetailView.as_view(), name="teacher_detail"),
    ]
    
    
    • (3)前端页面 teacher-detail.html
      课程讲师详情页有两个收藏按钮,分别是左侧的收藏讲师和右侧的收藏结构,这两个收藏是通过 ajax 实现的,同第7章的课程机构收藏功能是完全相同的,直接拷贝原有的代码即可.
      详情页的讲师排行榜也是和列表页讲师排行榜是相同的,代码一致.
    # templates/teacher-detail.html
    
    {% extends "base.html" %}
    {% load staticfiles %}
    
    {% block title %}讲师详情 - 慕学在线网{% endblock %}
    
    {% block custom_bread %}
        <section>
            <div class="wp">
                <ul  class="crumbs">
                    <li><a href="{% url "index" %}">首页</a>></li>
                    <li><a href="{% url "org:teacher_list" %}">授课讲师</a>></li>
                    <li>讲师详情</li>
                </ul>
            </div>
        </section>
    {% endblock custom_bread %}
    
    {% block content %}
        <section>
            <div class="wp butler-detail-box butler-diaryd-box clearfix">
                <div class="fl list">
                    <div class="butler_detail_list clearfix">
                        <div class="brief">
                            <dl class="des">
                                <dt>
                                    <div class="picpic">
                                        <img width="100" height="100" src="{{ MEDIA_URL }}{{ teacher.image }}"/>
                                    </div>
                                    <div class="btn">
                                        <span class="fl btn1 collectionbtn" id="jsLeftBtn">
                                             {% if has_teacher_favorite %}已收藏{% else %}收藏{% endif %}
                                        </span>
                                        <span class="fr btn2 shareBtn bdsharebuttonbox"
                                              data-text="授课教师-李老师-慕学网"
                                              data-desc="我在#慕课网#发现了"
                                              data-comment="李老师,工作年限:5年;学历:本科;所在公司:阿里巴巴&nbsp;;经典案例:django入门和深入;flask入门"
                                              data-url="/diary/hk_detail/10/">
                                            <span class="fl">分享</span>
                                            <a href="#" class="bds_more" data-cmd="more"></a>
                                        </span>
                                    </div>
                                </dt>
                                <dd>
                                    <a href="/diary/hk_detail/10/">
                                        <h1>{{ teacher.name }}<span class="key picbig">金牌讲师</span></h1>
                                    </a>
                                    <ul class="cont">
                                        <li>工作年限:<span>{{ teacher.work_years }}年</span></li>
                                        <li>就职公司:<span>{{ teacher.work_company }}</span></li>
                                        <li>工作职位:<span>{{ teacher.work_position }}&nbsp;</span></li>
                                        <li>教学特点:<span>{{ teacher.work_points }}</span></li>
                                    </ul>
                                </dd>
                            </dl>
                        </div>
                    </div>
                    <div class="butler_detail_cont clearfix">
                        <div class="left layout">
                        <div class="head">
                            <ul class="tab_header">
                                <li class="active"><a href="/diary/hk_detail/10/">全部课程</a> </li>
                            </ul>
                        </div>
                            <div class="companycenter">
                                <div class="group_list brief">
                                    {% for teacher_course in teacher_courses %}
                                        <div class="module1_5 box">
                                            <a href="{% url "course:course_detail" teacher_course.id %}">
                                                <img width="214" height="190" class="scrollLoading" src="{{ MEDIA_URL }}{{ teacher_course.image }}"/>
                                            </a>
                                            <div class="des">
                                                <a href="{% url "course:course_detail" teacher_course.id %}"><h2>{{ teacher_course.name }}</h2></a>
                                                <span class="fl">时长:<i class="key">{{ teacher_course.learn_times }}</i></span>
                                                <span class="fr">学习人数:{{ teacher_course.students }}</span>
                                            </div>
                                            <div class="bottom">
                                                <span class="fl">{{ teacher_course.course_org.name }}</span>
                                                <span class="star fr  notlogin" data-favid="15">{{ teacher_course.favorite_nums }}</span>
                                            </div>
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                        <!--<div class="pageturn">-->
                            <!--<ul class="pagelist">-->
                                <!--<li class="active"><a href="?page=1">1</a></li>-->
                            <!--</ul>-->
                        <!--</div>-->
                    </div>
                    </div>
                </div>
                <div class="fr list">
                     <div class="butler_detail_list">
                        <div class="right butler-company-box">
                        <div class="head">
                            <h1>{{ teacher.organization.name }}</h1>
                            <p>知名高校,权威教学</p>
                        </div>
                        <div class="pic">
                            <a href="{% url "org:org_home" teacher.organization.id %}">
                                <img width="150" height="80" src="{{ MEDIA_URL }}{{ teacher.organization.image }}"/>
                            </a>
                        </div>
                        <a href="{% url "org:org_home" teacher.organization.id %}">
                            <h2 class="center">{{ teacher.organization.name }}</h2>
                        </a>
                        <p class="center tell">地址:{{ teacher.organization.address }}</p>
                        <a class="btn" id="jsRightBtn">{% if has_organization_favorite %}已收藏{% else %}收藏{% endif %}</a>
                    </div>
                    </div>
                    <div class="butler_detail_cont">
                        <div class="butler_list_box">
                            <div class="right layout">
                                <div class="head">讲师排行榜</div>
                                {% for hot_teacher in sorted_teachers %}
                                    <dl class="des">
                                        <span class="num fl">1</span>
                                        <a href="{% url "org:teacher_detail" hot_teacher.id %}">
                                            <dt>
                                                <img width="50" height="50" class="scrollLoading"  src="{{ MEDIA_URL }}{{ hot_teacher.image }}"/>
                                            </dt>
                                        </a>
                                        <dd>
                                            <a href="{% url "org:teacher_detail" hot_teacher.id %}">
                                                <h1 title="{{ hot_teacher.name }}">{{ hot_teacher.name }}</h1>
                                            </a>
                                            <p>工作年限:<span>{{ hot_teacher.work_years }}年</span></p>
                                        </dd>
                                    </dl>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    {% endblock content %}
    
    {% block custom_js %}
    <script type="text/javascript">
        //收藏分享
        function add_favorite(current_elem, favorite_id, favorite_type){
            $.ajax({
                cache: false,
                type: "POST",
                url:"{% url "org:add_favorite" %}",
                data:{'favorite_id':favorite_id, 'favorite_type':favorite_type},
                async: true,
                // 因为用户点击"收藏"按钮后,向后台提交 POST 请求时,没有通过 form,
                // 不能在 form 中定义 {% csrf_token %}, 所以只能在这里把 {{ csrf_token }} 传递进来
                beforeSend:function(xhr, settings){
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success: function(data) {
                    var data = JSON.parse(data);
                    if(data.status == "fail"){
                        if(data.msg == "用户未登录"){
                            window.location.href="{% url "login" %}";
                        }else{
                            alert(data.msg);
                        }
    
                    }else if(data.status == 'success'){
                        current_elem.text(data.msg);
                    }
                },
            });
        }
    
        // 监听事件, 用来监听收藏按钮的点击操作
        $('#jsLeftBtn').on('click', function(){
            add_favorite($(this), {{ teacher.id }}, 3);
        });
    
        $('#jsRightBtn').on('click', function(){
            add_favorite($(this), {{ teacher.organization.id }}, 2);
        });
    
    </script>
    {% endblock custom_js %}
    
    

    相关文章

      网友评论

          本文标题:Django 开发 MxOnline 项目笔记 -- 第9章 课

          本文链接:https://www.haomeiwen.com/subject/kggbxftx.html