简体   繁体   English

建立分层注释树?

[英]build Hierarchical comment tree?

I am trying to implement threaded comments in my django project which I want to look like this: data 我正在尝试在我的django项目中实现线程注释,我想看起来像这样:data

comment_list = [
    {'id': 1, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 2, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 3, 'content': '...', 'pid': 1, 'children_comments': []},
    {'id': 4, 'content': '...', 'pid': 3, 'children_comments': []},
    {'id': 5, 'content': '...', 'pid': 4, 'children_comments': []},
    {'id': 6, 'content': '...', 'pid': 2, 'children_comments': []},
    {'id': 7, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 8, 'content': '...', 'pid': 7, 'children_comments': []},
    {'id': 9, 'content': '...', 'pid': None, 'children_comments': []},
    {'id': 10, 'content': '...', 'pid': 9, 'children_comments': []},
]

for example 例如

1
 3
  4
   5

2
 6

7
 8

9
 10

my code: 我的代码:

new = []
for comment in comment_list:
    if comment['pid'] != None:
        for i in comment_list:
            if i['id'] == comment['pid']:
                i['children_comments'].append(comment)
    else:
        new.append(comment)

i think that is not good 我认为那不好

final in django jinjia how to show?? 最终在django jinjia怎么显示?

Sure. 当然。

The algorithm is as follows: 算法如下:

  • iterate over the flat list of comments, gathering up comments by their parentage into a comments_by_parent mapping 遍历评论的平面列表,收集了由父母子女关系的意见为comments_by_parent映射
  • iterate over the list again, assigning children_comments from the comments_by_parent mapping 再次遍历该列表,从comments_by_parent映射中分配children_comments
  • get the root-level comments 获得根级别的评论

from collections import defaultdict

comment_list = [
    {'id': 1, 'content': '...', 'pid': None},
    {'id': 2, 'content': '...', 'pid': None},
    {'id': 3, 'content': '...', 'pid': 1},
    {'id': 4, 'content': '...', 'pid': 3},
    {'id': 5, 'content': '...', 'pid': 4},
    {'id': 6, 'content': '...', 'pid': 2},
    {'id': 7, 'content': '...', 'pid': None},
    {'id': 8, 'content': '...', 'pid': 7},
    {'id': 9, 'content': '...', 'pid': None},
    {'id': 10, 'content': '...', 'pid': 9},
]

comments_by_parent = defaultdict(list)
for comment in comment_list:
    comments_by_parent[comment['pid']].append(comment)

for comment in comment_list:
    comment['children_comments'] = comments_by_parent[comment['id']]

root_comments = comments_by_parent[None]

root_comments will end up looking like this (JSON output for clarity). root_comments最终将看起来像这样(为清晰起见,JSON输出)。

[
  {
    "id": 1,
    "content": "...",
    "pid": null,
    "children_comments": [
      {
        "id": 3,
        "content": "...",
        "pid": 1,
        "children_comments": [
          {
            "id": 4,
            "content": "...",
            "pid": 3,
            "children_comments": [
              {
                "id": 5,
                "content": "...",
                "pid": 4,
                "children_comments": []
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "id": 2,
    "content": "...",
    "pid": null,
    "children_comments": [
      {
        "id": 6,
        "content": "...",
        "pid": 2,
        "children_comments": []
      }
    ]
  },
  {
    "id": 7,
    "content": "...",
    "pid": null,
    "children_comments": [
      {
        "id": 8,
        "content": "...",
        "pid": 7,
        "children_comments": []
      }
    ]
  },
  {
    "id": 9,
    "content": "...",
    "pid": null,
    "children_comments": [
      {
        "id": 10,
        "content": "...",
        "pid": 9,
        "children_comments": []
      }
    ]
  }
]

You can then output this in Jinja using a recursive for loop: 然后,您可以使用递归for循环在Jinja中输出以下内容:

<ul>
    {%- for comment in root_comments recursive %}
        <li>
            {{ comment.content }}
            {%- if comment.children_comments -%}
                <ul>{{ loop(comment.children_comments) }}</ul>
            {%- endif %}
        </li>
    {%- endfor %}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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