不确定我做错了什么。我制作了一个评论系统,用户可以在帖子上发表评论并能够回复该评论。一切正常,除非用户回复某些评论时回复显示为单个评论。(并且它也显示为回复)所以如果我回复评论"你好吗&# 34;有了消息" good",然后我创建了两个" good"。一个作为答复。一个作为单一评论。我查看了我的代码,但我不知道为什么会发生这种情况。任何帮助将受到高度赞赏。谢谢/。这是我的代码。
views.py
def post(request, slug):
user = get_object_or_404(User,username__iexact=request.user)
try:
profile = MyProfile.objects.get(user_id=request.user.id)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
path = request.get_full_path()
comments = Comment.objects.filter(path=path)
#comments = post.comment_set.all()
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
parent_id = request.POST.get('parent_id')
parent_comment = None
if parent_id is not None:
try:
parent_comment = Comment.objects.get(id=parent_id)
except:
parent_comment = None
comment_text = comment_form.cleaned_data['comment']
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=request.get_full_path(),
text=comment_text,
post = post,
parent = parent_comment
)
comment_form = CommentForm()
return HttpResponseRedirect(post.get_absolute_url())
for c in comments:
c.get_children()
context_dict = {
'post' :post,
'profile' :profile,
'comments' : comments,
'comment_form':comment_form
}
return render(request, 'main/post.html', context_dict)
models.py
class Comment(models.Model):
def get_children(self):
if self.is_child:
return None
else:
return Comment.objects.filter(parent=self)
post.html
<h1>Comments/Questions</h1>
<form method="POST" action=''>{% csrf_token %}
{{ comment_form.as_p }}
<input type='submit' class='btn btn-default' value='Add comment'/>
</form>
<br/>
<hr/>
<table class='table'>
{% for comment in comments %}
<tr><td>{{ comment.get_comment }}
<br/><small>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago </small>
{% if not comment.is_child %}
<ul>
{% for child in comment.get_children %}
<li>{{ child.get_comment }}
<small>via {{ child.user }}</small>
</li>
{% endfor %}
</ul>
<a href='#' class='reply_btn'>Reply</a>
<div class='reply_comment'>
<form method="POST" action=''>{% csrf_token %}
<input type='hidden' name='parent_id' value='{{ comment.id }}' />
{{ comment_form.as_p }}
<input type='submit' class='btn btn-default' value='Add reply'/>
</form>
</div>
{% endif %}
</td></tr>
{% endfor %}
</table>
</div>
评论显示在post.html中,对于该页面,视图是
def post(request, slug):
user = get_object_or_404(User,username__iexact=request.user)
try:
profile = MyProfile.objects.get(user_id=request.user.id)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
path = request.get_full_path()
comments = Comment.objects.filter(path=path)
#comments = post.comment_set.all()
comment_form = CommentForm(request.POST or None)
for c in comments:
c.get_children()
context_dict = {
'post' :post,
'profile' :profile,
'comments' : comments,
'comment_form':comment_form
}
return render(request, 'main/post.html', context_dict)
答案 0 :(得分:0)
重复的评论只是模板的结果。
这种情况正在发生,因为当您循环浏览所有评论时,新制作的评论也会包含在comments
中。
除了充当评论子项之外,它还是一个评论对象,这就是当commments
使用提供的路径进行注释然后将其作为模板呈现时,它被包含在filter()
中的原因正常评论对象。
要解决此问题,您需要使用递归树。有像django-mptt
这样的django应用程序可以在这类问题中使用。
将其集成到您的评论模型后,您只需使用{% recursetree %}
模板标签即可呈现评论而无需重复。