------------------------------
课程列表
{% 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();
//刷新当前页面. } }, }); }); </script> 然后在后台if not request.user.is_authenticated(): # 判断用户登陆状态 json_dict = {'status': 'fail', 'msg': u'用户未登陆'} return HttpResponse(json.dumps(json_dict), content_type="application/json")
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")