输入评论内容,点击提交后,弹出“评论不能为空”。
来源:8-6 课程评论评论功能
ananyoung
2017-10-26
<!--发布评论--> <div id="js-pub-container" class="issques clearfix js-form"> <div class="wgt-ipt-wrap pub-editor-wrap " id="js-pl-input-fake"> <textarea id="js-pl-textarea" class="" placeholder="扯淡、吐槽、表扬、鼓励……想说啥就说啥!" ></textarea> </div> <input type="button" id="js-pl-submit" class="pub-btn" data-cid="452" value="发表评论"> <p class="global-errortip js-global-error"></p> </div> {% block custom_js %} <script type="text/javascript"> //添加评论 $('#js-pl-submit').on('click', function(){ var comments = $("#js-pl-textarea").text() if(comments == ""){ alert("评论不能为空") return } $.ajax({ cache: false, type: "POST", url:"{% url 'course:add_comment' course.id %}", 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.html"; }else{ alert(data.msg) } }else if(data.status == 'success'){ window.location.reload();//刷新当前页面. } }, }); }); </script> {% endblock %}
class AddCommentsView(View): """ 用户添加课程评论 """ def post(self, request): if not request.user.is_authenticated(): # 判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') course_id = request.POST.get("course_id", 0) comments = request.POST.get("comments", "") if course_id >0 and comments: course_comments = CourseComment() course = Course.objects.get(id=int(course_id)) course_comments.course = course course_comments.comments = comments course_comments.user = request.user course_comments.save() return HttpResponse('{"status":"success", "msg":"添加成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"添加失败"}', content_type='application/json')
urlpatterns = [ # 课程列表页 url(r'^list/$', CourseListView.as_view(), name="course_list"), #课程详情页 url(r'^detail/(?P<course_id>\d+)/$', CourseDetailView.as_view(), name="course_detail"), url(r'^info/(?P<course_id>\d+)/$', CourseInfoView.as_view(), name="course_info"), #课程评论 url(r'^comment/(?P<course_id>\d+)/$', CommentsView.as_view(), name="course_comments"), #添加课程评论 url(r'^add_comment/(?P<course_id>\d+)/$', AddCommentsView.as_view(), name="add_comment"), ] 请老师帮我看一下是哪边出现问题了,
写回答
1回答
-
ananyoung
提问者
2017-10-26
忘记放图片啦。。。,而且还有个问题就是右边的下载一栏,在章节页面可以显示资料名称,但是我按照章节中的代码修改评论页面后明确不能显示资料的名字,但是可以下载。
<ul class="downlist"> {% for course_resource in course_recources %} <li> <span ><i class="aui-iconfont aui-icon-file"></i> {{ course_recource.name }}</span> <a href="{{ MEDIA_URL }}{{ course_resource.download }}" class="downcode" target="_blank" download="" data-id="274" title="">下载</a> </li> {% endfor %} </ul>
022019-05-05
相似问题