32、Django实战第32天:我的收藏
我的收藏有3个页面:课程机构,授课教师,公开课程
课程机构
1、编辑usercenter-fav-org.html继承usercenter-base.html
2、编辑users.views.py
1 2 3 4 5 6 7 8 9 10 11 12 13 | ... from operation.models import UserFavorite class MyFavOrgView(LoginRequiredMixin, View): def get( self , request): user_orgs = UserFavorite.objects. filter (user = request.user, fav_type = 2 ) org_ids = [user_org.fav_id for user_org in user_orgs] orgs = CourseOrg.objects. filter (id__in = org_ids) return render(request, 'usercenter-fav-org.html' , { 'orgs' : orgs, }) |
3、编辑users.urls.py
1 2 3 4 5 6 7 8 | ... from .views import MyFavOrgView urlpatterns = [ ... url(r 'myfav_org/$' , MyFavOrgView.as_view(), name = 'myfav_org' ), ] |
4、编辑usercenter-base.html修改导航链接
5、编辑usercenter-fav-org.html数据动态展示
授课教师
1、编辑usercenter-fav-teacher.html继承usercenter-base.html
2、编辑users.views.py
1 2 3 4 5 6 7 8 9 10 11 12 | ... from organization.models import Teacher class MyFavTeacherView(View): def get( self , request): fav_teachers = UserFavorite.objects. filter (user = request.user, fav_type = 3 ) teacher_ids = [fav_teacher.fav_id for fav_teacher in fav_teachers] teachers = Teacher.objects. filter (id__in = teacher_ids) return render(request, 'usercenter-fav-teacher.html' ,{ 'teachers' : teachers, }) |
3、编辑users.urls.py
1 2 3 4 5 6 7 8 | ... from .views import MyFavTeacherView urlpatterns = [ ... url(r 'myfav_teacher/$' , MyFavTeacherView.as_view(), name = 'myfav_teacher' ), ] |
4、编辑usercenter-fav-teacher.html和usercenter-fav-org.html修改导航链接
5、编辑usercenter-fav-teacher.html动态数据展示
公开课程
1、编辑usercenter-fav-course.html,继承usercenter-base.html
2、users.views.py
1 2 3 4 5 6 7 8 | class MyFavCourseView(LoginRequiredMixin, View): def get( self , request): fav_courses = UserFavorite.objects. filter (user = request.user, fav_type = 1 ) course_ids = [fav_course.fav_id for fav_course in fav_courses] courses = Course.objects. filter (id__in = course_ids) return render(request, 'usercenter-fav-course.html' , { 'courses' : courses, }) |
3、编辑users.urls.py
1 2 3 4 5 6 7 8 | ... from .views import MyFavCourseView urlpatterns = [ ... url(r 'myfav_course/$' , MyFavCourseView.as_view(), name = 'myfav_course' ), ] |
4、编辑usercenter-fav-course.html
删除收藏
这个功能可以直接调用我们之前写的皆苦
修改usercenter-fav-org.html
修改usercenter-fav-teacher.html
修改usercenter-fav-course.html
编辑usercenter-base.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <script type = "text/javascript" > $( '.jsDeleteFav_course' ).on( 'click' , function(){ var _this = $(this), favid = _this.attr( 'data-favid' ); alert(favid) $.ajax({ cache: false, type : "POST" , url: "{% url 'org:add_fav' %}" , data: { fav_type: 1 , fav_id: favid, csrfmiddlewaretoken: '{{ csrf_token }}' }, async: true, success: function(data) { Dml.fun.winReload(); } }); }); $( '.jsDeleteFav_teacher' ).on( 'click' , function(){ var _this = $(this), favid = _this.attr( 'data-favid' ); $.ajax({ cache: false, type : "POST" , url: "{% url 'org:add_fav' %}" , data: { fav_type: 3 , fav_id: favid, csrfmiddlewaretoken: '{{ csrf_token }}' }, async: true, success: function(data) { Dml.fun.winReload(); } }); }); $( '.jsDeleteFav_org' ).on( 'click' , function(){ var _this = $(this), favid = _this.attr( 'data-favid' ); $.ajax({ cache: false, type : "POST" , url: "{% url 'org:add_fav' %}" , data: { fav_type: 2 , fav_id: favid, csrfmiddlewaretoken: '{{ csrf_token }}' }, async: true, success: function(data) { Dml.fun.winReload(); } }); }); < / script> |
每天进步一点,加油!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步