課程詳情頁面
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
結(jié)構(gòu): content中有兩個(gè)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 %}
配置課程列表超鏈接至課程詳細(xì)
打開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">學(xué)習(xí)人數(shù):{{course.students}}</span>
</div>
<ul class="parameter">
<li><span class="pram word3">時(shí) 長:</span><span>{{ course.learn_times }}</span></li>
<li><span class="pram word3">章 節(jié) 數(shù):</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">學(xué)習(xí)用戶:</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">開始學(xué)習(xí)</a></div>
</div>
</div>
其中{{ course.lesson_nums }}章節(jié)數(shù)是通過Course類中的lesson_nums 方法動(dòng)態(tài)統(tǒng)計(jì)章節(jié)數(shù)字
課程收藏和機(jī)構(gòu)收藏展示實(shí)現(xiàn)
在course應(yīng)用中的views.py中更新如下代碼
from apps.operations.models import UserFavorite
# 獲取收藏狀態(tài)
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
用戶收藏點(diǎn)擊實(shí)現(xiàn)
打開course-detail.html添加ajax請(qǐng)求代碼
{% 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 %}
無論點(diǎn)擊左側(cè)收藏和右側(cè)收藏都是請(qǐng)op下面的fav
項(xiàng)目urls增加
# 用戶相關(guān)操作
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"]
#是否已經(jīng)收藏
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": "參數(shù)錯(cuò)誤"
})
相關(guān)課程推薦(單標(biāo)簽版)
# 通過課程的tag做課程的推薦
tag = course.tag
related_courses = []
if tag:
related_courses = Course.objects.filter(tag=tag).exclude(id__in=[course.id])[:3]
前臺(tái)
<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">學(xué)習(xí)時(shí)長:<i class="key">{{ course.learn_times }}</i></span>
</dd>
</dl>
{% endfor %}
</div>