学习笔记第七天

------------------------------

课程列表

{% block ........ %}

{%  endblock %}#模版的继承

对于分页后,会对前端传递{all_courses:courses},创建courses=Paginator(后台数据,每页数据个数,request=request)

前端遍历all_courses时得加上---.object_list


某一属性是有choices=?

degree=models.CharField(verbose_name=u'难度',choices=(("cj","初级"),("zj","中级"),("gj","高级")),max_length=2)
的话,在前端引用得.get_degree_display才能显示“初级”而不是“cj”

----------------------------

课程详情页

b为a的外键的话,可以通过 a.b.属性 来获取b的属性

在models.py中突然添加新字段(有外键的)后默认值不能为空,在类中给它添加null=True,blank=True

---------------------------

课程章节信息

类.object.get()#get返回一个数据

类.object.filter()#filter返回满足条件的全部数据,返回数组

----------------------------

课程评论

首先在html中设置Ajax的jQuery

 #添加评论
     <script type="text/javascript">
    //添加评论
    $('#js-pl-submit').on('click', function(){
        var comments = $("#js-pl-textarea").val()
        if(comments == ""){
            alert("评论不能为空")
            return
        }
        $.ajax({
            cache: false,
            type: "POST",
            url:"{% url 'course:add_comment' %}",
            data:{'course_id':{{ course.id }}, 'comments':comments},
            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="/login/";
                    }else{
                        alert(data.msg)
                    }

                }else if(data.status == 'success'){
                    window.location.reload();
        if not request.user.is_authenticated():
            # 判断用户登陆状态
            json_dict = {'status': 'fail', 'msg': u'用户未登陆'}
            return HttpResponse(json.dumps(json_dict), content_type="application/json")
//刷新当前页面. } }, }); }); </script>


然后在后台

def post(self,request):

        if not request.user.is_authenticated():
            # 判断用户登陆状态
            json_dict = {'status': 'fail', 'msg': u'用户未登陆'}
            return HttpResponse(json.dumps(json_dict), content_type="application/json")

        course_id=request.POST.get("course_id",0)
        comments=request.POST.get("comments","")
        if course_id>0 and comments:
	#判断哪门课以及相应评论,建立实例进行储存CourseComments()
            course_comments=CourseComments()
            course=Course.objects.get(id=int(course_id))
            course_comments.course=course
            course_comments.comments=comments
            course_comments.user=request.user
            course_comments.save()
            json_dict = {'status': 'success', 'msg': u'添加成功'}
            return HttpResponse(json.dumps(json_dict), content_type="application/json")
        else:
            json_dict = {'status': 'success', 'msg': u'添加失败'}
            return HttpResponse(json.dumps(json_dict), content_type="application/json")













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值