简体   繁体   中英

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 ).
it will be something like this, add it to your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM