32、Django实战第32天:我的收藏

我的收藏有3个页面:课程机构,授课教师,公开课程

 

课程机构

 1、编辑usercenter-fav-org.html继承usercenter-base.html

2、编辑users.views.py

...
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

...
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

...
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

...
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

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

...
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

<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>
posted @ 2018-03-27 17:38  sellsa  阅读(202)  评论(0编辑  收藏  举报