递归解压缩Django模板中的列表

时间:2013-01-22 21:50:52

标签: django

我有以下列表,我想在我的模板中解压缩(这存储在eachpart.1

[u'Value1', u'Value2', Value3, Value4, [u'Value1', u'Value2', Value3, Value4], [u'Value1', u'Value2', Value3, Value4]]

大部分时间格式都是这样的:

[u'Value1', u'Value2', Value3, Value4]

这在我的模板中显示如下:

<tr>
    <td width = 150px><a href="#">{{eachpart.0}}</a></td>
    <td>{{eachpart.1.0}}</td>
    <td><img src = "eachpart.1.1}}.jpg" alt="{{eachpart.1.1}}">
    </td>
</tr>

但是每隔一段时间我就会把角落的情况放在顶部,这可能是未知的重复。

我在模板中检测到这一点如下:

 {% if eachpart.1|length > 4 %}

这告诉我,我有其中一个角落案例。所以现在我想基本确定条目的数量(将是eachpart.1|length - 1),并且对于每个条目,我想使用计数器来访问正确的值。

这可能在模板中吗?是否可以在模板中执行while循环和计数器?

或者我是否以完全低效的方式解决这个问题?

0 个答案:

没有答案