Answer the question
In order to leave comments, you need to log in
Why is django template ignoring id in dictionary?
Purpose: to make the conclusion of comments with multiple innateness.
To do this, a list is made for the initial comments (main_comments), looks something like this
context['main_comments'] = [<Comments: 23>, <Comments: 24>]
context['child_comments'] = {24: [<Comments: 22>, <Comments: 25>], 22: [<Comments: 26>]}
{% with perent_comment_id=comment.id %}
{% include "post/child_comments.html" %}
{% endwith %}
{%for comment in child_comments.perent_comment_id%}
{# вывод каждого комментария #}
{{perent_comment_id}} {# вывод родительского id: 24 #}
{{child_comments.24}} {# вывод дочерних комментариев: [<Comments: 22>, <Comments: 25>] #}
{{child_comments.perent_comment_id}} {# не выводит ничего #}
{%for key, comments in child_comments.items%}
{% if key == perent_comment_id %}
{%for comment in comments%
{# вывод каждого комментария #}
Answer the question
In order to leave comments, you need to log in
1. You need your model with comments to have a key for itself
parent = models.ForeignKey("self")
2. You need to form in view, for example, such a list with
PS data: this is done using a recursive function, google (how to recursively form a tree, approximately)
PS2: to a list maybe also include parent_id and whatever else is needed
comments = [{
'id': 1,
'text': 'asdaasd',
'childrens': []
},
{ 'id': 2,
'text': 'asdaasd',
'childrens': [
{ 'id': 7,
'text': 'asdaasd',
'childrens': [
{ 'id': 12,
'text': 'asdaasd',
'childrens': [
{ 'id': 23,
'text': 'asdaasd',
'childrens': [],
},
... # и т.д.
],
},
... # и т.д.
],
},
{ 'id': 8,
'text': 'asdaasd',
'childrens': [],
},
... # и т.д.
],
},
{ 'id': 2,
'text': 'asdaasd',
'childrens': [
{ 'id': 15,
'text': 'asdaasd',
'childrens': [],
},
{ 'id': 19,
'text': 'asdaasd',
'childrens': [],
},
... # и т.д.
],
}
... # и т.д.
]
{%for comment in comments%}
{{ comment.id }}
{{ comment.text }}
{% if comment.childrens %}
# По идее здесь тоже надо будет организовать рекурсию, для отображения множественной вложенности
# гугли - django template recursive tree, примерно
# К вложенным комментариям вот так можно обращаться будет
{% for children in comment.childrens %}
{{ children.id }}
{{ children.text }}
{% if children.childrens %}
# и т.д.
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question