html - 姜戈。回复作为评论发布的评论。如何获得评论的回复?
问题描述
嘿,我对 django 很陌生,我有评论和回复的功能,问题是我不能回复评论,而是作为评论发布。如何在各自的评论下得到这个回复?这是我的模型和功能。
模型
class Comment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
reply = models.ForeignKey('Comment', null=True, related_name='replies', blank=True, on_delete=models.CASCADE)
content = models.TextField(max_length=1000)
timestamp = models.DateTimeField(auto_now_add=True)
意见
def comment_section(request, slug):
user = request.user
post = get_object_or_404(Post, slug=slug)
comments = Comment.objects.filter(post=post, reply=None).order_by('-id')
if request.POST:
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
content = request.POST.get('content')
reply_id = request.POST.get('comment_id') #reply-section
comment_qs = None
if reply_id:
comment_qs = Comment.objects.get(id=reply_id)
comment = Comment.objects.create(post=post, user=request.user, content=content, reply=comment_qs)
comment.save()
else:
comment_form = CommentForm()
context = {
'post':post,
'comment_form':comment_form,
'comments': comments,
}
if request.is_ajax():
html = render_to_string('posts/comment_section.html', context, request=request)
return JsonResponse({'form': html})
html
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action="{% url 'posts:comment_section' post.slug %}">
{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-warning" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{% if not post.comment_set.all %}
<small>No comments to display</small>
{% endif %}
{% for comment in post.comment_set.all %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><a href="{% url 'posts:userprofile' comment.user.username %}" style="text-decoration: none; color: black;"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6></a><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><i class="fas fa-reply fa-sm"></i></a> 
{% if request.user == comment.user %}
<a href="{% url 'posts:delete_comment' comment.id %}"><i class="fas fa-trash fa-sm" style="color: brown;"></i></a></td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><a href="" style="text-decoration: none; color: black;"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6></a><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action="{% url 'posts:comment_section' post.slug %}">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>
我想表单提交存在一些问题,但我无法弄清楚它是什么。谢谢
解决方案
这就是我轻松解决问题的方法,它在很多方面对我有帮助。
评论 = Comment.objects.filter(post=post, reply=None).order_by('-id')
{% 用于评论中的评论 %}
{% if comment.reply %}
IF 语句将检查评论是否是回复,这是否有父级 如果是,您可以使用 {{comment.reply.content}} 和 {{comment.reply.timestamp}} 访问它以打印父级。和 {{comment.content}} 打印回复。
{% 别的 %}
即,如果评论只是评论而不是回复。只需打印评论 {{comment.content}}
{% endfor %}
推荐阅读
- python - 通过在字典中附加熊猫数据框来创建字典
- javascript - Youtube API 密钥配额在使用有问题的密钥之前超过了
- python - Squish 测试:通过 Squish 获取活动窗口或顶部窗口
- python - python包安装不保存?
- python - 当 pip 卸载不起作用时,如何卸载库?
- php - 从地址创建超链接
- flutter - 反转图像颜色
- mysql - Combine one table with meta data in another table
- python - 用 Pandas 列中的句点替换空格
- docker - 有没有办法在谷歌云构建的 yaml 构建配置文件中的同一步骤中分离不同的参数?