![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
'''
def comment_tree(request,article_id):
ret=list(models.Comment.objects.filter(article_id=article_id).values("pk","content","parent_comment_id"))
print(ret)
return JsonResponse(ret,safe=False)
// 获取评论数据,展示评论树结构
$.ajax({
url: "/blog/comment_tree/" + '{{ article.pk }}/',
success: function (data) {
console.log(data);
$.each(data, function (index, comment_dict) {
var s = '<div class="comment_item" comment_id=' + comment_dict.pk + '> <span class="content">' + comment_dict.content + '</span> </div>'
if (comment_dict.parent_comment_id) {
// 子评论
pid=comment_dict.parent_comment_id;
$("[comment_id="+pid+"]").append(s);
}
else { // 根评论
$(".comment_tree").append(s);
}
})
}
});
'''
View Code
![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
'''
{% block page-main %}
<div class="article-detail">
<h1>{{ article.title }}</h1>
{# <h3>{{ article.desc }}</h3>#}
<p>{{ article.articledetail.content|safe }}</p>
</div>
<div class="poll clearfix">
<div id="div_digg">
<div class="diggit action">
<span class="diggnum" id="digg_count">{{ article.up_count }}</span>
</div>
<div class="buryit action">
<span class="burynum" id="bury_count">{{ article.down_count }}</span>
</div>
<div class="clear"></div>
<div class="diggword" id="digg_tips" style="color: red;"></div>
</div>
</div>
<p>评论列表</p>
<ul class="comment_list">
{% for comment in comment_list %}
<li class="list-group-item">
<div>
<a href="">#{{ forloop.counter }}楼</a>
<span style="color: gray">{{ comment.create_time|date:"Y-m-d H:i" }}</span>
<a href=""><span>{{ comment.user.username }}</span></a>
<a class="pull-right reply_btn" username="{{ comment.user.username }}"
comment_pk="{{ comment.pk }}"><span>回复</span></a>
</div>
{# 父评论 #}
{% if comment.parent_comment_id %}
<div class="pid_info well">
<p> {{ comment.parent_comment.user.username }}:
{{ comment.parent_comment.content }} </p>
</div>
{% endif %}
<div class="con">
<p>
{{ comment.content }}
</p>
</div>
</li>
{% endfor %}
</ul>
//评论功能
{% if request.user.username %}
<div class="div_comment">
<p>昵称:<input type="text" id="tbCommentAuthor" class="author" disabled="disabled" size="50"
value="{{ request.user.username }}"></p>
<p>评论内容</p>
<textarea name="" id="comment_content" cols="60" rows="10"></textarea>
<p>
<button id="comment_btn" style="margin-top: 20px">提交评论</button>
</p>
</div>
{% else %}
<a href="/login/">登录</a>
{% endif %}
{#自定义一个属性标签,便用后续使用属性值,并且当js设置为静态文件时,时必须要用到的 #}
<div class="info" article_id="{{ article.pk }}" username="{{ request.user.username }}"></div>
{% endblock %}
{% block page_js %}
{% csrf_token %}
<script>
$("#div_digg .action").on("click",function () {
//判断是点赞还是踩灭,注意不能是diggnum
if($(".info").attr("username")){
var is_up=$(this).hasClass("diggit");
console.log(is_up);
//获取到文章id 在js中需要加引号
var article_id="{{ article.pk }}";
$.ajax({
url:"/blog/up_down/",
type:"post",
data:{
"is_up":is_up,
"article_id":article_id,
"csrfmiddlewaretoken":$("[name='csrfmiddlewaretoken']").val(),
},
success:function(data){
alert(data);
//如果为真,说明是第一次操作
//console.log(data.state); 都可以使用
console.log(data["state"]);
if(data.state){
if(is_up){
var val=$("#digg_count").text(); //点赞加1
val=parseInt(val)+1;
console.log(val);
$("#digg_count").text(val);
}else{
var val=$("#bury_count").text();
val=parseInt(val)+1;
$("#bury_count").text(val); //踩加1
}
}else{
var val=66;
console.log(val);
if(data.first_action){
$("#digg_tips").html("您已经推荐过");
}else {
$("#digg_tips").html("您已经反对过");
}
}
}
}
)
}else{
location.href = "/login/"
}
});
//提交评论 pid为空就是根评论,不为空就是子评论
var pid="";
$("#comment_btn").on("click",function () {
var article_id='{{ article.pk }}';
var content=$("#comment_content").val();
console.log(article_id,content);
if(pid) {
//子评论时,要有@父评论用户,然后再填评论,在存评论时只存评论内容,因此用到切割
var index=content.indexOf("
");
content=content.slice(index+1)
}
$.ajax({
url:"/blog/comment/",
type:"post",
data:{
article_id:article_id,
content:content,
pid:pid,
csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
},
success:function (data) {
alert(data)
var create_time=data.create_time;
var content=data.content;
var username=data.username;
//拼接使用++
var comment_li= '<li class="list-group-item"><div><span style="color: gray">' + create_time + '</span> <a href=""><span>' + username + '</span></a></div> <div class="con"> <p> ' + content + ' </p> </div> </li>';
$(".comment_list").append(comment_li);
// 清空文本框
$("#comment_content").val('');
// 清空pid
pid = ""
}
})
});
//回复功能
$(".list-group-item .reply_btn").on("click",function () {
//点击回复的时候,光标移到评论框
$("#comment_content").focus();
//得到属性为username的值
var v="@"+$(this).attr("username")+"
";
$("#comment_content").val(v)
//pid赋值
pid = $(this).attr("comment_pk")
})
</script>
{% endblock %}
'''
HTML
![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
![评论功能和render渲染
1.render渲染
2.评论功能
3.评论树
4.评论功能代码]()
'''
def article_detail(request, username, pk):
"""
:param username: 被访问的blog的用户名
:param pk: 访问的文章的主键id值
:return:
"""
user = models.UserInfo.objects.filter(username=username).first()
if not user:
return HttpResponse("404")
blog = user.blog
# 找到当前的文章
article_obj = models.Article.objects.filter(pk=pk).first()
# 所有评论列表
comment_list = models.Comment.objects.filter(article_id=pk)
return render(
request,
"article_detail.html",
{
"username": username,
"article": article_obj,
"blog": blog,
"comment_list": comment_list
}
)
def comment(request):
if request.method=='POST':
article_id=request.POST.get("article_id")
pid=request.POST.get("pid")
content=request.POST.get("content")
user_pk=request.user.pk
response = {}
if not pid: #此时是根评论
comment_obj=models.Comment.objects.create(article_id=article_id,user_id=user_pk,content=content)
else: #此时是子评论,要添加父评论的id
comment_obj=models.Comment.objects.create(article_id=article_id, user_id=user_pk, content=content,parent_comment_id=pid)
response["create_time"]=comment_obj.create_time.strftime("%Y-%m-%d") #comment_obj.create_time是个日期对象,需要转换
response["content"]= content
response["username"] = comment_obj.user.username
return JsonResponse(response)
'''
View