在Django temp中显示引导列表组中的数据

2024-03-28 22:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有汽车的数据,我希望它们显示在引导列表组中。问题是我希望一个汽车品牌只展示一次。

{u'cars': [{u'brand': u'Ford', u'model': u'Focus'}, {u'brand': u'u'Ford', u'model': u'Fiesta'}, {u'brand': u'u'Toyota', u'model': u'Hilux'}]

我把车列在视图.py公司名称:

^{pr2}$

所以,在这个例子中,我希望在引导列表组中显示如下:http://www.bootply.com/XEnAquIInD

不是这样的: http://www.bootply.com/2YX7PgB1ch

我在Django模板中遇到了这个问题。当我在模板中循环汽车时,我需要为列表项在HTML中有不同的数据父ID。另外,我怎么能检查,没有一个以上的汽车品牌显示?在

<div id="MainMenu">
        <div class="list-group panel">
          <div href="#demo" class="list-group-item list-group-item-success"  data-parent="#MainMenu">Laitteet</div>
          <div class="collapse in" id="demo">

          {% for car in cars %}

          <a href="#{{ ??? }}" class="list-group-item" data-toggle="collapse" data-parent="#{{ ??? }}">{{ car.brand }} <i class="fa fa-caret-down"></i></a>

            <div class="collapse list-group-submenu" id="{{ ??? }}">
              <a href="#" class="list-group-item" data-parent="#{{ ??? }}">{{ car.model }}</a>
            </div>
        {% endfor %}
      </div>
  </div>
</div>

Tags: divid列表datamodelgroupitemcar
1条回答
网友
1楼 · 发布于 2024-03-28 22:28:48

我会做一些类似的事情来清理您视图中的数据:

cars_seen = set()
for i in readable_json["cars"]:
    car_identifier = '{}-{}'.format(i['brand'], i['model'])
    if not car_identifier in cars_seen:
        cars.append({
            'brand': i['brand'],
            'model': i['model'],
            'id': car_identifier,
        })
    cars_seen.add(car_identifier)

cars现在将包含一个独特汽车的列表(假设brand+model是如何定义唯一性的)。在

在模板中,您可以使用以下内容生成唯一的ID:

^{pr2}$

相关问题 更多 >