在Django temp中通过多个数组进行parstring

2024-10-02 20:43:16 发布

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

我有一个原始的SQL输出,其中也包含一些数组。我想解析这些行并遍历其中的数组。你知道吗

我写了以下代码:

     {% for row in in results %}
                 <li>Title : {{row.4}}</li>
                 <li>Description :{{ row.2}}</li>
                 {% for i in row.3|length|range %}
                      <li class="bs-callout">
                         <button id="{{row.3.forloop.counter0}}">{{row.4.forloop.counter0}}</button>
                         <span>{{row.5.forloop.counter0}}</span>
                     </li>
                  {% endfor%}
     {% endfor %}

样本行:

['Col1','Col2',['txt1','txt2','txt3'],['txt1','txt2','txt3'],['txt1','txt2','txt3']]

在本例中,row[5],row[6],row[7]是包含5-7个值的数组。 对于每一行,我想打印row[4]中的标题并列出数组项。你知道吗


Tags: 代码inforsqlbuttonli数组results
1条回答
网友
1楼 · 发布于 2024-10-02 20:43:16

我不知道这样做是否正确。但这是唯一对我有用的东西。你知道吗

    {% for row in in results %}
       <li>Title : {{row.4}}</li>
       <li>Description :{{ row.2}}</li>
       {% for i in row.3|length|range %}
            <li class="bs-callout">
               <button id="{{row.3|getitem:i}}">{{row.4|getitem:i}}</button>
               <span>{{row.5|getitem:i}}</span>
            </li>
       {% endfor%}
    {% endfor %}

其中getitem是一个自定义筛选器。你知道吗

@register.filter(name='getitem')
def get_item( list,offset ):
    return list[offset]

相关问题 更多 >