Django+xadmin打造在线教育平台

Posted 老街旧人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django+xadmin打造在线教育平台相关的知识,希望对你有一定的参考价值。

Django+xadmin打造在线教育平台(五)

 

代码

github下载

八、课程详情页功能的实现

8.1.课程列表

 (1)配置urls

MxOnline/urls中

path("course/", include(\'course.urls\', namespace="course")),

course里面新建urls.py

复制代码
# course/urls.py

from django.urls import path,re_path
from .views import CourseListView

# 要写上app的名字
app_name = "course"

urlpatterns = [
    path(\'list/\',CourseListView.as_view(),name=\'course_list\'),

]
复制代码

把course-list.html拷贝到templates目录下

复制代码
from django.shortcuts import render
from django.views.generic import View

class CourseListView(View):
    def get(self, request):
        return render(request, "course-list.html")
复制代码

 

(2)course-list.html继承base.html

修改title,修改bread里面,content里面放course-list独有的

 course-list.html

然后去后台添加十门课程

(3)列表展示

views.py

复制代码
# course/views.py

from django.shortcuts import render
from django.views.generic import View
from .models import Course

class CourseListView(View):
    def get(self, request):
        all_courses = Course.objects.all()

        return render(request, "course-list.html",{\'all_courses\':all_courses})
复制代码

course-list.html

复制代码
 <div class="group_list">
    {% for course in all_course %}
    <div class="box">
        <a href="course-detail.html">
            <img width="280" height="350" class="scrollLoading" src="{{ MEDIA_URL }}{{ course.image }}"/>
        </a>
        <div class="des">
            <a href="course-detail.html">
                <h2>{{ course.name }}</h2>
            </a>
            <span class="fl">时长:<i class="key">{{ course.learn_times }}</i></span>
            <span class="fr">学习人数:{{ course.students }}&nbsp;&nbsp;</span>
        </div>
        <div class="bottom">
            <a href="course-detail.html"><span class="fl">来自{{ course.course_org.name }}</span></a>
            <span class="star fr  notlogin
                " data-favid="15">
                {{ course.fav_nums }}
            </span>
        </div>
    </div>
    {% endfor %}
复制代码

8.2.分页

复制代码
        try:
            page = request.GET.get(\'page\', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_courses,2 , request=request)
        courses = p.page(page)
复制代码
复制代码
<div class="pageturn">
                        <ul class="pagelist">
                            {% if all_courses.has_previous %}
                                <li class="long"><a href="?{{ all_courses.previous_page_number.querystring }}">上一页</a></li>
                            {% endif %}

                            {% for page in all_courses.pages %}
                                {% if page %}
                                    {% ifequal page all_courses.number %}
                                        <li class="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_courses.has_next %}
                                <li class="long"><a href="?{{ all_courses.next_page_number.querystring }}">下一页</a></li>
                            {% endif %}
                        </ul>
                    </div>
复制代码

8.3.排序

复制代码
class CourseListView(View):
    def get(self, request):
        all_courses = Course.objects.all().order_by(\'-add_time\')
        # 热门课程推荐
        hot_courses = Course.objects.all().order_by(\'-click_nums\')[:3]
        # 排序
        sort = request.GET.get(\'sort\', "")
        if sort:
            if sort == "students":
                all_courses = all_courses.order_by("-students")
            elif sort == "hot":
                all_courses = all_courses.order_by("-click_nums")
        # 分页
        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,

        })
复制代码
复制代码
<div class="head">
                    <ul class="tab_header">
                        <li class="{% ifequal sort \'\' %}active{% endifequal %}"><a href="?sort=" >最新 </a></li>
                        <li class="{% ifequal sort \'hot\' %}active{% endifequal %}"><a href="?sort=hot" >最热门 </a></li>
                        <li class="{% ifequal sort \'students\' %}active{% endifequal %}"><a href="?sort=students" >参与人数 </a></li>
                    </ul>
                </div>
复制代码
复制代码
<div class="head">热门课程推荐</div>
                <div class="group_recommend">
                    {% for hot_course in hot_courses %}
                    <dl>
                        <dt>
                            <a target="_blank" href="">
                                <img width="240" height="220" class="scrollLoading" src="{{ MEDIA_URL }}{{ hot_course.image }}"/>
                            </a>
                        </dt>
                        <dd>
                            <a target="_blank" href=""><h2> {{ hot_course.name }}</h2></a>
                            <span class="fl">难度:<i class="key">{{ hot_course.get_degree_display }}</i></span>
                        </dd>
                    </dl>
                    {% endfor %}
复制代码
 course-list.html
 views.py

 

8.4.课程详情

course-detail.html复制进来

 

 (1)url配置

re_path(\'course/(?P<course_id>\\d+)/\', CourseDetailView.as_view(), name="course_detail"),
复制代码
class CourseDetailView(View):
    \'\'\'课程详情\'\'\'
    def get(self, request, course_id):
        return  render(request, "course-detail.html", {

        })
复制代码

 

 在course-list.html中添加链接到详情

 

 (2)views

复制代码
class CourseDetailView(View):
    \'\'\'课程详情\'\'\'
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        # 课程的点击数加1
        course.click_nums += 1
        course.save()
        return  render(request, "course-detail.html", {
            \'course\':course,

        })
复制代码

(3)Course model增加

  • 一个category字段
  • 一个获取章节数的方法
  • 一个获取这么课程的学习用户方法

 

 Course

(4)course-detail.html中课程详情信息显示

复制代码
<div class="picbox">
                        <div class="tb-booth tb-pic">
                            <img width="440" height="445" src="{{ MEDIA_URL }}{{ course.image }}" class="jqzoom" />
                        </div>

                    </div>
                    <div class="des">
                        <h1 title="{{ course.name }}">{{ course.name }}</h1>
                        <span class="key">{{ course.desc }}</span>
                        <div class="prize">
                            <span class="fl">难度:<i class="key">{{ course.get_degree_display }}</i></span>
                            <span class="fr">学习人数:{{ course.students }}</span>
                        </div>
                        <ul class="parameter">
                            <li><span class="pram word3">时&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;长:</span><span>{{ course.learn_times }}</span></li>
                            <li><span class="pram word3">章&nbsp;节&nbsp;数:</span><span>{{ course.get_zj_nums }}</span></li>
                            <li><span class="pram word3">课程类别:</span><span title="">{{ course.category }}</span></li>
                            <li class="piclist"><span class="pram word4">学习用户:</span>
                                {% for user_course in course.get_learn_users %}
                                    <span class="pic"><img width="40" height="40" src="{{ MEDIA_URL }}{{ user_course.user.image }}"/></span>
                                {% endfor %}
                            </li>
                        </ul>
                        <div class="btns">
复制代码

 

 显示课程详情

 

 

8.5.授课机构 

 (1)CourseOrg model添加一个获取教师数的方法

    def get_teacher_nums(self):
        #获取机构的教师数
        return self.teacher_set.all().count()
 CourseOrg

(2)授课机构显示

复制代码
<div class="head">
                        <h1>授课机构</h1>
                        <p>世界名校,课程权威</p>
                    </div>
                    <div class="pic">
                        <a href="/company/14/">
                            <img width="150" height="80" src="{{ MEDIA_URL }}{{ course.course_org.image }}"/>
                        </a>
                    </div>
                    <a href="/company/14/">
                        <h2 class="center" title="清华大学">{{ course.course_org.name }}</h2>
                    </a>
                    <div class="btn  notlogin
                         "data-favid="14" id="jsRightBtn">
                         已收藏
                    </div>
                    <div class="clear">
                        <ul>
                            <li>
                                <span>课 &nbsp;程&nbsp; 数:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   {{ course.course_org.course_nums }}</span>
                            </li>
                            <li>
                                <span>教 &nbsp;师&nbsp; 数:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {{ course.course_org.get_teacher_nums }}</span>
                            </li>
                            <li>所在地区:&nbsp;&nbsp;{{ course.course_org.address }}</li>
                            <li>认&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;证&nbsp;:
                                &nbsp;&nbsp;
                                    <img title="金牌机构", src="{% static \'images/gold.png\' %}"/>
                            </li>
                        </ul>
                    </div>
                </div>
复制代码

 

 

8.6.相关课程推荐

(1)给“Course” model添加一个“课程标签”字段

tag = models.CharField(\'课程标签\',default=\'\',max_length=10)
 Course

 (2)views

复制代码
class CourseDetailView(View):
    \'\'\'课程详情\'\'\'
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        # 课程的点击数加1
        course.click_nums += 1
        course.save()
        # 课程标签
        # 通过当前标签,查找数据库中的课程
        tag = course.tag
        if tag:
            # 需要从1开始不然会推荐自己
            relate_courses = Course.objects.filter(tag=tag)[:3]
        else:
            relate_courses = []
        return  render(request, "course-detail.html", {
            \'course\':course,
            \'relate_courses\':relate_courses,
        })
复制代码

(3)前端

复制代码
<div class="right layout">

                <div class="head">相关课程推荐</div>
                <div class="group_recommend">
                        {% for relate_course in relate_courses %}
                        <dl>
                            <dt>
                                <a target="_blank" href="">
                                    <img width="240" height="220" class="scrollLoading" src="{{ MEDIA_URL }}{{ relate_course.image }}"/>
                                </a>
                            </dt>
                            <dd>
                                <a target="_blank" href=""><h2> {{ relate_course.name }}</h2></a>
                                <span class="fl">学习时长:<i class="key">{{ relate_course.learn_times }}</i></span>
                            </dd>
                        </dl>
                        {% endfor %}
                </div>
            </div>
复制代码

 

 

 

8.7.课程收藏和机构收藏

  {% block custom_js %}{% endblock %},放到最下面的位置,因为是js代码,要最后加载

后端判断当前收藏转态

复制代码
class CourseDetailView(View):
    \'\'\'课程详情\'\'\'
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        # 课程的点击数加1
        course.click_nums += 1
        course.save()
        # 课程标签
        # 通过当前标签,查找数据库中的课程
        has_fav_course = False
        has_fav_org = False

        # 必须是用户已登录我们才需要判断。
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
                has_fav_course = True
            if UserFavorite.objects.filter(user=request.user, fav_id=course.course_org.id, fav_type=2):
                has_fav_org = True
        tag = course.tag
        if tag:
            # 需要从1开始不然会推荐自己
            relate_courses = Course.objects.filter(tag=tag)[:2]
        else:
            relate_courses = []
        return  render(request, "course-detail.html", {
            \'course\':course,
            \'relate_courses\':relate_courses,
            "has_fav_course": has_fav_course,
            "has_fav_org": has_fav_org,
        })
复制代码

course-detail.html中添加Ajax

复制代码
{% block custom_js %}
    <script type="text/javascript">
//收藏分享
function add_fav(current_elem, fav_id, fav_type){
    $.ajax({
        cache: false,
        type: "POST",
        url:"{% url "org:add_fav" %}",
        data:{\'fav_id\':fav_id, \'fav_type\':fav_type},
        async: true,
        beforeSend:function(xhr, settings){
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
        },
        success: function(data) {
            if(data.status == \'fail\'){
                if(data.msg == \'用户未登录\'){
                    window.location.href="/login/";
                }else{
                    alert(data.msg)
                }

            }else if(data.status == \'success\'){
                current_elem.text(data.msg)
            }
        },
    });
}

$(\'#jsLeftBtn\').on(\'click\', function(){
    add_fav($(this), {{ course.id }}, 1);
});

$(\'#jsRightBtn\').on(\'click\', function(){
    add_fav($(this), {{ course.course_org.id }}, 2);
});


</script>

{% endblock %}
复制代码

以上是关于Django+xadmin打造在线教育平台的主要内容,如果未能解决你的问题,请参考以下文章

Django+xadmin打造在线教育平台

Django+xadmin打造在线教育平台

Django+xadmin打造在线教育平台

Django+xadmin打造在线教育平台

第三百七十九节,Django+Xadmin打造上线标准的在线教育平台—xadmin的安装

Django+xadmin打造在线教育平台