课程详情页面
course app中 urls
from apps.courses.views import CourseDetailView
url(r'^(?P<course_id>\d+)/$', CourseDetailView.as_view(), name="detail"),
course app中 views
class CourseDetailView(View):
def get(self, request, course_id, *args, **kwargs):
"""
获取课程详情
"""
course = Course.objects.get(id=int(course_id))
course.click_nums += 1
course.save()
return render(request, "course-detail.html", {
"course":course,
})
course-detail.html
结构: content中有两个section
面包屑
{% block custom_bread %}
<section>
<div class="wp">
<ul class="crumbs">
<li><a href="{% url 'index' %}">首页</a>></li>
<li><a href="{% url 'course:list' %}">公开课</a>></li>
<li>课程详情</li>
</ul>
</div>
</section>
{% endblock %}
配置课程列表超链接至课程详细
打开course-list.html
<div class="left">
<div class="picbox">
<div class="tb-booth tb-pic">
<img width="440" height="445" src="{{course.image.url }}" 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">时 长:</span><span>{{ course.learn_times }}</span></li>
<li><span class="pram word3">章 节 数:</span><span>{{ course.lesson_nums }}</span></li>
<li><span class="pram word3">课程类别:</span><span title="">{{ course.category }}</span></li>
<li class="piclist"><span class="pram word4">学习用户:</span>
<span class="pic"><img width="40" height="40" src="/static/media/image/2016/12/default_big_14.png"/></span>
</li>
</ul>
<div class="btns">
<div class="btn colectgroupbtn" id="jsLeftBtn">
收藏
</div>
<div class="buy btn"><a style="color: white" href="course-video.html">开始学习</a></div>
</div>
</div>
其中{{ course.lesson_nums }}章节数是通过Course类中的lesson_nums 方法动态统计章节数字
课程收藏和机构收藏展示实现
在course应用中的views.py中更新如下代码
from apps.operations.models import UserFavorite
# 获取收藏状态
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
用户收藏点击实现
打开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 'op: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="{% url 'login' %}";
}else{
alert(data.msg)
}
}else if(data.status == 'success'){
current_elem.text(data.msg)
}
},
});
}
$(document).ready(function() {
$('#jsLeftBtn').on('click', function () {
add_fav($(this), {{ course.id }}, 1);
});
});
$(document).ready(function() {
$('#jsRightBtn').on('click', function(){
add_fav($(this), {{ course.course_org.id }}, 2);
});
});
</script>
{% endblock %}
无论点击左侧收藏和右侧收藏都是请op下面的fav
项目urls增加
# 用户相关操作
url(r'^op/', include(('apps.operations.urls', "operations"), namespace="op")),
operations下面urls
from django.conf.urls import url
from apps.operations.views import AddFavView
urlpatterns = [
url(r'^fav/$', AddFavView.as_view(), name="fav"),
]
forms.py
import re
from django import forms
from apps.operations.models import UserFavorite
class UserFavForm(forms.ModelForm):
class Meta:
model = UserFavorite
fields = ["fav_id", "fav_type"]
views.py
from django.views.generic import View
from django.http import JsonResponse
from django.shortcuts import render
from apps.operations.forms import UserFavForm
from apps.operations.models import UserFavorite
from apps.courses.models import Course
from apps.organizations.models import CourseOrg, Teacher
# Create your views here.
class AddFavView(View):
def post(self, request, *args, **kwargs):
"""
用户收藏,取消收藏
"""
#先判断用户是否登录
if not request.user.is_authenticated:
return JsonResponse({
"status":"fail",
"msg":"用户未登录"
})
user_fav_form = UserFavForm(request.POST)
if user_fav_form.is_valid():
fav_id = user_fav_form.cleaned_data["fav_id"]
fav_type = user_fav_form.cleaned_data["fav_type"]
#是否已经收藏
existed_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type)
if existed_records:
existed_records.delete()
if fav_type == 1:
course = Course.objects.get(id=fav_id)
course.fav_nums -= 1
course.save()
elif fav_type == 2:
course_org = CourseOrg.objects.get(id=fav_id)
course_org.fav_nums -= 1
course_org.save()
elif fav_type == 3:
teacher = Teacher.objects.get(id=fav_id)
teacher.fav_nums -= 1
teacher.save()
return JsonResponse({
"status": "success",
"msg": "收藏"
})
else:
user_fav = UserFavorite()
user_fav.fav_id = fav_id
user_fav.fav_type = fav_type
user_fav.user = request.user
user_fav.save()
return JsonResponse({
"status": "success",
"msg": "已收藏"
})
else:
return JsonResponse({
"status": "fail",
"msg": "参数错误"
})
相关课程推荐(单标签版)
# 通过课程的tag做课程的推荐
tag = course.tag
related_courses = []
if tag:
related_courses = Course.objects.filter(tag=tag).exclude(id__in=[course.id])[:3]
前台
<div class="group_recommend">
{% for course in related_courses %}
<dl>
<dt>
<a target="_blank" href="">
<img width="240" height="220" class="scrollLoading" src="{{ course.image.url }}"/>
</a>
</dt>
<dd>
<a target="_blank" href=""><h2> {{ course.name }}</h2></a>
<span class="fl">学习时长:<i class="key">{{ course.learn_times }}</i></span>
</dd>
</dl>
{% endfor %}
</div>