1
我試圖做一個自定義的評論引擎,但我想不出如何顯示嵌套的評論。我使用'回覆'ForeignKey來跟蹤它引用的評論。我正在使用關卡字段來查看它是什麼「級別」評論。如何在Django樣式模板中收集子註釋?
models.py:
class Post(models.Model)
name = models.CharField()
text = models.TextFiled()
class Comment(models.Model)
o_post = models.ForeignKey(Post)
reply = models.ForeignKey('self', blank=True, null=True)
level = models.IntegerField(default=1)
#others like content,author, created etc...
views.py
def PostComments(request,postpk):
post = Post.objects.get(pk=postpk)
comments = Comment.objects.filter(o_post=post).order_by('-created')
children = Comment.objects.filter(o_post=post).filter(level__gte=2)
context = {'comments':comments,'post':post,'children':children}
return render_response(stuff)
這裏是我如何努力,以顯示一切。所有1級評論都可見。 child.reply返回一個ID,所以沒有comment.pk,它們都匹配41
{% for comment in comments %}
{{comment.content}}
{% for child in children %}
{%if child.reply == comment.pk %}
{{child.content}}
{% endif %}
{% endfor %}
{% endfor %}
無論我如何結構的,如果循環我無法弄清楚如何得到它的工作。由於
什麼!工作!但是不應該41 41的答案也通過? – theptrk
我相信你會比較和回覆實例41號碼 – mikes000
打印像{{child.reply}}這樣的實體可能會因爲__str __()或__repr __()而顯示「41」顯示。這並不意味着實體只是一個數字。你也可以使用'{%if child.reply.id == comment.pk%}'。 –