I am trying to implement threaded comments in my django project which I want to look like this: 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??
Sure.
The algorithm is as follows:
comments_by_parent mappingchildren_comments from the comments_by_parent mappingfrom 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).
[
{
"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:
<ul>
{%- for comment in root_comments recursive %}
<li>
{{ comment.content }}
{%- if comment.children_comments -%}
<ul>{{ loop(comment.children_comments) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With