首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有动态内容的html中的树结构

具有动态内容的html中的树结构
EN

Stack Overflow用户
提问于 2019-02-22 20:00:35
回答 1查看 932关注 0票数 0

在执行查询时,我有来自数据库的数据,格式如下

代码语言:javascript
复制
[(‘Country-1’, ‘state1’), (‘Country-1’, ‘state2’), (‘Country-1’, ‘state3’),
(‘Country-2’, ‘state1’), (‘Country-2’, ‘state2’), (‘Country-2’, ‘state3’),
(‘Country-3’, ‘state1’), (‘Country-3’, ‘state2’), (‘Country-3’, ‘state3’)]

我想转换为如下格式的结果集

代码语言:javascript
复制
context = { 
    'countries': [ { 'Countryname': 'country1’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    },
                    { 'Countryname': 'country2’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    }, 
                    { 'Countryname': 'country3’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    }
                ]
}

这样我就可以在Django中的in中迭代数据来创建树格式:

代码语言:javascript
复制
<ul class = "myUL">
  {% for country in data %}
            <li class = "caret"> {{ country.countryname }} </li>
            <ul class="nested">
              {% for state in country.statename %}
                <li>{{state.statename}}</li>
                {% endfor %}
            </ul>
  {% endfor %}

HTML的预期输出是:

代码语言:javascript
复制
   Country-1  
             State1
             State2
             State3 
   Country -2 
             State1
             State2
             State3 
   Country -3 
             State1
             State2
             State3 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-22 21:17:35

尝试以下几点:

数据解析器:

代码语言:javascript
复制
data = [('Country-1', 'state1'), ('Country-1', 'state2'), ('Country-1', 'state3'), ('Country-2', 'state1'), ('Country-2', 'state2'), ('Country-2', 'state3'), ('Country-3', 'state1'), ('Country-3', 'state2'), ('Country-3', 'state3')]

reformatted_data = {}

for pair in data:

    state_list = reformatted_data.get(pair[0], None)

    if state_list:
        if pair[1] in state_list:
            pass
        else:
            reformatted_data[pair[0]].append(pair[1])
    else:
        reformatted_data[pair[0]] = [pair[1]]

# Try this print in your console to make sure it's working properly
print(reformatted_data)

显示数据的模板:

代码语言:javascript
复制
<ul class = "myUL">
  {% for country, statelist in data.items %}
            <li class = "caret"> {{ country }} </li>
            <ul class="nested">
              {% for state in statelist %}
                <li>{{ state }}</li>
                {% endfor %}
            </ul>
  {% endfor %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54834319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档