提交评论功能

This commit is contained in:
2024-05-06 12:35:09 +08:00
parent 645aafcadb
commit e48d34c52c
10 changed files with 215 additions and 8 deletions
+107
View File
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章详情</title>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/jsmind.js"></script>
<script src="/static/js/jsmind.draggable.js"></script>
<script src="/static/js/jsmind.screenshot.js"></script>
<script src="/static/js/jsmind.menu.js"></script>
<script src="/static/js/jsmind.coop.js?d="></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="/static/js/mind/c_m_m.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script> <!-- 图标相关-->
<style>
.article_detail{
width: 80%;
margin-left: 100px;
}
.comment_post{
width: 80%;
margin-left: 100px;
}
.comment_show{
width: 80%;
margin-left: 100px;
}
#commentform_title {
background-repeat: no-repeat;
padding: 0 0 0 25px;
margin-bottom: 10px;
}
.feedback_area_title {
border-bottom: 1px solid #ddd;
font-size: 14px;
font-weight: bold;
margin: 20px 0 10px;
}
#p{
border: deepskyblue 1px dashed;
background-color: antiquewhite;
}
</style>
</head>
<body>
<div class="comment_post"> {# 提交评论区域的div #}
<div id="commentform_title">发表评论</div>
<textarea rows="10" cols="60" id="comment_content"></textarea>
<p><button>提交评论</button></p>
</div>
<div class="comment_show"> {# 评论展示区域的div #}
<div class="feedback_area_title">评论列表</div>
<div class="comment_list">
{% for comment in comment_list %} {# 循环展示评论的数据 #}
<div>
<p>第{{ forloop.counter}}楼 -> By:{{ comment.user.username }} -> {{ comment.comment_time }} -> <button class="reply" username={{ comment.user.username }} pk={{ comment.pk }}>回复</button></p> {# 在此处定义一个回复按钮,用户实现子评论,并且自定义属性username和pk,用于下面回复功能的实现 #}
{% if comment.pre_comment_id %} {# 判断评论是否有父级评论 #}
<p id="p">原评论内容:{{ comment.pre_comment.comment_content }}</p> {# 如果有父级评论,则在中间显示父级评论的评论内容 #}
{% endif %}
<p>评论内容:{{ comment.comment_content }}</p>
<hr>
</div>
{% endfor %}
</div>
</div>
{# 提交评论的JS,发送Ajax请求 #}
<script>
var pid = "" {# 设置一个变量pid默认为空,用于后面作为数据库存储的父级评论的ID,如果没有父级评论则为空,子评论有父级评论 #}
{# 提交评论按钮的点击事件 #}
$(".comment_post p button").click(function (){
$.ajax({
url: '/comment_control/',
headers: {"X-CSRFToken": $.cookie("csrftoken")},
type: 'post',
data: {
comment_content: $("#comment_content").val(), {# 提交的数据内容data #}
article_id: {{ path }},
pid: pid
},
success: function (res){ {# 本例中返回的数据仅仅用于在控制台打印而已 #}
console.log(res) {# 控制台打印返回的数据 #}
$("#comment_content").val("") {# 提交完成后,清空评论输入框的内容 #}
pid = "" {# 子评论提交完成后,将pid默认设置为空,恢复为默认的父评论 #}
}
})
})
{# 回复按钮的点击事件 #}
$(".reply").click(function (){
$("#comment_content").focus() {# 回复按钮的事件,点击时,将光标聚集到评论输入框中 #}
var val = "@" + $(this).attr("username") + "\n" {# $(this)指代".reply"标签本身,获取这个标签的username值 #}
$("#comment_content").val(val) {# 回复时,自动在输入框加入:@要回复的人 #}
pid = $(this).attr("pk") {# 当点击回复时,父评论ID不再为空,此时修改为:对应评论的主键值ID #}
})
</script>
</body>
</html>