首页 > 解决方案 > django 用重复的 nametuple 重组

问题描述

我在 django 模板中有重新组合的代码,如下所示:

        {% regroup clients by title.0 as clients_list %}
          {% for client in clients_list %}
            {{ client.grouper }}
              {% for item in client.list %}
                  {% if item.client_link %}
                        <a href="{{ item.client_link }}">{{ item.title }}</a> 
                  {% else %}
                        {{ item.title }}
                  {% endif %}
              {% endfor %}
          {% endfor %}

有时我的结果中有相同字母的重复名称元组,例如 - 'd':

GroupedResult(grouper='d', list=[<Client: decompany_1>]) GroupedResult(grouper='d', list=[<Client: decompany_2>])

所以结果我有两个带有字母“d”的标题

有人知道,如何避免这种情况?

编辑 视图.py

class ClientsListView(ListView):
    model = Client
    template_name = 'clients/clients_list.html'
    context_object_name = 'clients'

标签: pythondjangotemplatesalphabetical

解决方案


推荐阅读