小编Ale*_*lex的帖子

使用forloop.counter值作为Django模板中的列表索引

在我的Django 1.1.1应用程序中,我在视图中有一个函数,它向模板返回一系列数字和一系列项目列表,例如:

...  
data=[[item1 , item2, item3], [item4, item5, item6], [item7, item8, item9]]  
return render_to_response('page.html', {'data':data, 'cycle':range(0,len(data)-1])
Run Code Online (Sandbox Code Playgroud)

在模板内部我有一个外部for循环,其中还包含另一个循环以在输出中显示以这种方式包含内部数据列表

...  
{% for page in cycle %}   
...   
< table >   
{% for item in data.forloop.counter0 %}  
< tr >< td >{{item.a}} < /td > < td > {{item.b}} ... < /td > < /tr >  
...  
< /table >  
{% endfor %}  
{% if not forloop.last %}  
< div class="page_break_div" >  
{% endif %}  
{% endfor %}  
... 
Run Code Online (Sandbox Code Playgroud)

但是Django模板引擎不能将forloop.counter0值作为列表的索引(相反,如果我手动将数值作为索引).有没有办法让列表循环与外部 …

django templates for-loop list django-templates

17
推荐指数
2
解决办法
4万
查看次数

标签 统计

django ×1

django-templates ×1

for-loop ×1

list ×1

templates ×1