[英]how to create comment tree in django
I need a comment system so can reply a comment to any comment我需要一个评论系统,所以可以回复任何评论的评论
I know how to write models and views and my only problem is showing them in the template我知道如何编写模型和视图,我唯一的问题是在模板中显示它们
For example, maybe a group of comments are like this:例如,也许一组评论是这样的:
comment
comment
comment
comment
comment
comment
comment
comment
comment
comment
How can I display this structure in the template?如何在模板中显示此结构?
Your Comment
model should have a parent
field which refers to another comment( self relationship ).您的
Comment
model 应该有一个parent
字段引用另一个评论(自我关系)。
it will be something like this, add it to your Comment
model:它将是这样的,将其添加到您的
Comment
model:
parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')
now you have your replies and even your replies can be the parent of another comment.现在您有了回复,甚至您的回复也可以成为另一条评论的父级。
And in your template:在您的模板中:
{% for replay in comment.replies.all %}
<p class="info">{{ replay.user }} | {{ replay.date }}</p>
<li>{{ replay.text }}</li>
{% endfor %}
Note that the field names are just examples请注意,字段名称只是示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.