U
U
Untiwe2021-01-15 02:52:49
Django
Untiwe, 2021-01-15 02:52:49

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>]

And a dictionary of child comments (child_comments), where the key is the id of the parent comment. looks something like this
context['child_comments'] = {24: [<Comments: 22>, <Comments: 25>], 22: [<Comments: 26>]}

When displaying the main comment, the template for displaying the child is connected, and a variable is set for it to refer to the dictionary. It looks like this
{% with perent_comment_id=comment.id %}
    {% include "post/child_comments.html" %}
{% endwith %}

And in the template, we loop through the dictionary, accessing the dictionary by the key perent_comment_id
{%for comment in child_comments.perent_comment_id%}
    {# вывод каждого комментария #}

But it doesn't work!
Now let's take a look at our hands:
{{perent_comment_id}} {# вывод родительского id: 24 #}
{{child_comments.24}} {# вывод дочерних комментариев: [<Comments: 22>, <Comments: 25>] #} 
{{child_comments.perent_comment_id}} {# не выводит ничего #}

Now the question is how? and what to do?

PS
You have to iterate over the dictionary every time and compare the key with the perent_comment_id. It works but the optimization is bad
{%for key, comments in child_comments.items%}
    {% if key == perent_comment_id %}
        {%for comment in comments%
             {# вывод каждого комментария #}


PSS I know about django-mptt but I love making bikes and understanding them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WStanley, 2021-01-15
@Untiwe

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': [],
        },
        ... # и т.д.
    ],
}
... # и т.д.
]

3. After that, throw it into the template, and in the template it will be possible to do this:
PS: where the list of childrens is empty, then there are no comments-answers
{%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 %}

This is how all this should work if you use a reference to yourself
PS: in general, to work with a tree, correctly use nested sets, which is done in django-mptt, but that's a completely different story :)
Happy cycling!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question