'''
{% 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("\n");
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")+"\n";
$("#comment_content").val(v)
//pid賦值
pid = $(this).attr("comment_pk")
})
</script>
{% endblock %}
'''