简体   繁体   English

递归浏览django模板中的字典

[英]Recursively navigating through dictionary in the django template

I have a dictionary of unknown depth which I want to display in my Django template.我有一本深度未知的字典,我想在我的 Django 模板中显示它。 This dictionary represents the structure of the subfolders inside of a folder.该字典表示文件夹内子文件夹的结构。 My goal is to display it with the parent subfolders before their children, which would be indented to show that they're children of that folder.我的目标是将其与父子文件夹一起显示在其子文件夹之前,这将缩进以显示它们是该文件夹的子文件夹。

So to explain with an example, if I have the following dictionary:所以用一个例子来解释,如果我有以下字典:

{'Script Hello': 
    {'Script Hello World': 
        {'Script Hello World 2': None, 
         'Script Hello World 3': None}}, 
 'Script Hello World 4': None, 
 'Script Hello World 5': 
    {'Script Hello World 6': None}}

I want it to be displayed like this :我希望它像这样显示:

. Script Hello
    . Script Hello World
        . Script Hello World 2
        . Script Hello World 3

. Script Hello World 4

. Script Hello World 5
    . Script Hello World 6

I've done a small code that does the job if the depth if at most 3 (to determine what I really wanted), but couldn't find a way to make it recursive.如果深度最多为 3(以确定我真正想要的),我已经完成了一个小代码,但找不到使其递归的方法。

My code is this :我的代码是这样的:

{% for key, values in list.items %}
    <li> {{ key }}
        <ul>
            {% for key,values in values.items %}
                <li>{{ key }}</li>
                    <ul>
                        {% for key,values in values.items %}
                            <li>{{ key }}</li>
                        {% endfor %}
                    </ul>
            {% endfor %}
        </ul>
    </li>
{% endfor %}

I tried using Javascript to make a quick function to navigate through my dictionary and display its content, however I couldn't find how to get my dictionary as is in my Js function, I was always getting it escaped and wasn't able to use it, even after trying solutions like this one .我尝试使用 Javascript 制作一个快速函数来浏览我的字典并显示其内容,但是我找不到如何在我的 Js 函数中获取我的字典,我总是让它转义并且无法使用它,即使在尝试了这样的解决方案之后。

So I'd like your help in order to display my dictionary in my template recursively, which I didn't manage to do, even after trying all I could think of.因此,我需要您的帮助,以便在我的模板中递归地显示我的字典,即使在尝试了我能想到的所有方法之后,我也无法做到。

I think the simplest solution is to use a custom template tag:我认为最简单的解决方案是使用自定义模板标签:

from django.utils.safestring import mark_safe

def get_data(data):
    text = "<ul>"
    for k, v in data.items():
       text += f'<li>{k}<li>'
       if isinstance(v, dict):
           text += get_data(v)  # recursively calling to get the lists
       else:  # this else block can be removed if you don't need it
           text += f'<li>{v}</li>'
    text += "</ul>"
    return text
       

@register.filter(is_safe=True)
def render_dict(data):
    text = get_data(data)
    return mark_safe(text)

More information can be found in the documentation .更多信息可以在文档中找到。

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

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