我正在博客中进行评论/回复系统。如果对该评论有任何答复,则显示为1条答复等。单击链接将打开答复。
<a class="comment-reply-btn" href="#" > Reply </a>
点击回复可切换包含回复的div,否则将使用CSS隐藏该div。
div是:-
<div class="comment-reply ml-5 mt-1">
{% for child_comment in comment.children %}
<img class="rounded-circle" alt="{{child_comment.user.get_full_name }}" src="{{child_comment.user.userprofile.get_user_image}}" height="18px" width="18px">
<h6 class="d-inline ml-3">{{ child_comment.user.get_full_name }}: </h6>
<span>{{ child_comment.content }}</span>
<footer>{{ child_comment.timestamp|timesince }} ago</footer>
{% endfor %}
{% if request.user.is_authenticated %}
<form method="POST" action="."> {% csrf_token %}
{{ comment_form|crispy }}
<input type='hidden' name='parent_pk' value='{{ comment.pk }}'>
<input type='submit' value='Reply' class='btn btn-default'>
</form>
{% else %}
<p>You must login to reply</p>
{% endif %}
</div>
我正在使用以下CSS设置来隐藏div,使其无法在页面加载中显示:-
.comment-reply{
display: none;
}
和以下js通过单击回复按钮将其打开
$(".comment-reply-btn").click(function(event){
event.preventDefault();
$(this).parent().next(".comment-reply").fadeToggle();
})
我的问题是,如果我登录了,它会打开答复并以表格形式编写新的答复。但是,如果我没有登录,则单击“答复”按钮不会打开答复(应该如此)。
请帮助。
答案 0 :(得分:1)
您必须使用js处理每种情况所显示的内容。 首先在js中定义全局变量:
var user_is_authenticated = {{ request.user.is_authenticated|yesno:"true,false" }};
您可以使用变量来处理代码。像这样的东西:
if (user_is_authenticated === true) {
// append comments html code to your section
}
else {
// don't show the comments
}