mxonline实战15,用户中心:我的课程, 我的收藏,我的消息,登出和点击数以及收藏数
对应github地址:第15天
一. 我的课程
1. 继承usercenter-base页面
2. 编写url, view
3. usercenter-mycourse.html中填充课程信息
需要注意的是课程链接标签<a href="{% url 'course:course_detail' user_course.course.id %}",课程id前面要写user_course
4. usercenter-base.html中添加跳转链接
二. 我的收藏
1. 以usercenter-fav-org.html为例,
1). 继承usercenter-base.html
2). 编写ulr,view
users/urls.py添加
3). 填充课程机构信息
4). 在usercenter-base.html中配置我的收藏跳转地址
2. 相似的操作改写usercenter-fav-teacher.html
在usercenter-fav-teacher.html中,填充字段课程数时,由于在Teacher字段没有定义课程数字段,但是在courses/models.py->Courses中,定义了一个外键指向Teacher
所以可在organization/models->class Teacher中加上
然后在前端中直接使用此函数
3. usercenter-fav-course.html类似
3.1 需要注意的是3个收藏类型的跳转,在3个收藏页面都要这么写
3.2 在前端收藏有一个删除的图标,是由js完成的,需要注意的是下面代码中data-favid="{{ course.id }}用于完成删除收藏,其他页面也要传入相应ID
相应的删除收藏的js代码放在usercenter-base.html中,没事看看
三. 我的消息
1. usercenter-message.html继承base页面
2. 在usercenter-base.html中配置跳转到我的消息链接
3. 配置url和view
同时在usercenter-message.html中添加分页代码
users/urls.py添加
4. users/views.py->RegisterView中添加用户注册时的欢迎消息
userMessage定义查看如下
四. 页面顶部小喇叭未读消息数量通知
users/models.py-> UserProfile中增加
base.html中大概48行添加如下代码,这个就是小喇叭的效果
然后在其他base页面中都添加如上小喇叭的代码
五. 页面顶部个人中心退出
1. 编写view,URL
users/views.py中新加
说明:
1) 设计的目的是点击退出后跳转到index.html页面,
2) django中有一个页面跳转函数HttpResponseRedirect,from django.http import HttpResponse, HttpResponseRedirect
3) django中reverse函数,用于把url地址别名解析为url地址。
4) 用户退出时,直接用logout函数实现。from django.contrib.auth import authenticate, login, logout
users/urls.py中写入
2. 在base.html等3个base页面为退出添加链接
六. 实现机构,课程,讲师的点击数增加功能
1. 点击课程开始学习后,参与人数增加1,course/views.py->CourseInfoView中增加
2. organization/views.py -> TeacherDetailView中
3. 机构点击后click_nums加1,organization/views.py -> orgHomeView
七. 机构,课程,讲师的收藏数量动态改变
organization/views.py -> AddFavView。当取消收藏时,因为之前我们收藏没加收藏数的逻辑,所以即使收藏了收藏数也是0,那么取消收藏的时候就会变成负数。需要加个判断
class AddFavView(View):
def post(self, request):
# 获得从前端传来的fav_id, fav_type
fav_id = request.POST.get('fav_id', 0)
fav_type = request.POST.get('fav_type', 0)
# 判断用户是否登陆,request有一个匿名类user,可用于判断用户是否登陆
if not request.user.is_authenticated:
# 如果返回fail,ajax中自动跳转等登录页面,里面的msg值要和ajax中的值相同
return HttpResponse('{"status": "fail", "msg":"用户未登录"}', content_type='application/json')
exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
# 如果记录已存在,就取消收藏
if exist_records:
exist_records.delete()
# 收藏数减去1
if int(fav_type) == 1:
course = Course.objects.get(id=int(fav_id))
course.fav_nums -= 1
if course.fav_nums < 0:
course.fav_nums = 0
course.save()
elif int(fav_type) == 2:
org = CourseOrg.objects.get(id=int(fav_id))
org.fav_nums -= 1
if org.fav_nums < 0:
org.fav_nums = 0
org.save()
elif int(fav_type) == 3:
teacher = Teacher.objects.get(id=int(fav_id))
teacher.fav_nums -= 1
if teacher.fav_nums < 0:
teacher.fav_nums = 0
teacher.save()
return HttpResponse('{"status": "fail", "msg":收藏"}', content_type='application/json')
# 记录不存在,就收藏
else:
user_fav = UserFavorite()
# 过滤掉没取到fav_id, fav_type的默认情况
if int(fav_type) > 0 and int(fav_id) > 0:
user_fav.fav_id = int(fav_id)
user_fav.fav_type = int(fav_type)
user_fav.user = request.user
user_fav.save()
# 收藏数加1
if int(fav_type) == 1:
course = Course.objects.get(id=int(fav_id))
course.fav_nums += 1
course.save()
elif int(fav_type) == 2:
org = CourseOrg.objects.get(id=int(fav_id))
org.fav_nums += 1
org.save()
elif int(fav_type) == 3:
teacher = Teacher.objects.get(id=int(fav_id))
teacher.fav_nums += 1
teacher.save()
return HttpResponse('{"status": "success", "msg":"已收藏"}', content_type='application/json')
else:
return HttpResponse('{"status": "fail", "msg":"收藏出错"}', content_type='application/json')
八. 用户进入个人消息后,修改小喇叭消息已读
users/views.py->MyMessageView增加
努力生活,融于自然