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

在Django模板中使用forloop.counter值作为列表索引,django,list,templates,for-loop,django-templates,Django,List,Templates,For Loop,Django Templates,在我的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]) 在模板内部,我有一个外部for循环,它还包含另一个

在我的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])
在模板内部,我有一个外部for循环,它还包含另一个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 %}  
... 
。。。
{循环%中的页的%s}
...   

{data.forloop.counter0%}

...  
{{item.a}{{{item.b}}/td>
{%endfor%} {%if不是forloop.last%}
{%endif%} {%endfor%} ...
但是Django模板引擎不使用
forloop.counter0
值作为列表的索引(如果我手动将数值作为索引,它会这样做)。有没有办法让列表循环与外部
forloop.counter0
值一起工作?
提前感谢您的帮助:)

您不能将变量用于属性名称、字典键或列表索引

另外,
range(0,len(数据)-1]
不是有效的python。它应该是
range(len(数据))

您可能不需要
循环
。可能您想要的是:

{% for itemlist in data %}
    ...
    <table>
        {% for item in itemlist %}
        <tr>
          <td>{{ item.a }}</td>
          <td>{{ item.b }} ... </td>
        </tr>
        ...
        {% endfor %}
    </table>
    {% if not forloop.last %}
        <div class="page_break_div">
    {% endif %}
{% endfor %}
{%用于数据%中的itemlist}
...
{itemlist%中项目的%s}
{{item.a}
{{item.b}}。。。
...
{%endfor%}
{%if不是forloop.last%}
{%endif%}
{%endfor%}

我以一种相当低效的方式解决了这个问题。阅读此代码时,请不要呕吐在计算机上。给定两个长度相同的列表,它将遍历第一个列表,并从第二个列表打印相应的项目

如果必须使用此选项,请仅将其用于两个列表长度都很小的很少访问的模板。理想情况下,重构模板的数据以完全避免此问题

{% for list1item in list1 %}
   {% for list2item in list2 %}
      {% if forloop.counter == forloop.parentloop.counter %}
          {{ list1item }} {{ list2item }}
      {% endif %}
   {% endfor %}
{% endfor %}

我想在我的表格中使用样式表,通过传递一个切换真/假值的列表来实现颜色的交替。我发现这非常令人沮丧。最后,我创建了一个字典项列表,其中的键与表格中的字段相同,另外还有一个是切换真/假值

def jobListView(request):
    # django does not allow you to append stuff to the job identity, neither
    # will it allow forloop.counter to index another list. The only solution
    # is to have the toggle embedded in a dictionary along with
    # every field from the job
    j                   = job.objects.order_by('-priority')
    # have a toggling true/false list for alternating colours in the table
    theTog              = True
    jobList             = [] 
    for i in j:
        myJob           = {}
        myJob['id']     = i.id
        myJob['duty']   = i.duty
        myJob['updated'] = i.updated
        myJob['priority'] = i.priority
        myJob['description'] = i.description
        myJob['toggle'] = theTog
        jobList.append(myJob)
        theTog          = not(theTog)
    # next i

    return render_to_response('index.html', locals())
# end jobDetaiView
还有我的模板

{% if jobList %}
    <table border="1"><tr>
    <th>Job ID</th><th>Duty</th><th>Updated</th><th>Priority</th><th>Description</th>
    </tr>

    {% for myJob in jobList %}

        <!-- only show jobs that are not closed and have a positive priority. -->
        {% if myJob.priority and not myJob.closeDate %}
            <!-- alternate colours with the classes defined in the style sheet -->
            {% if myJob.toggle %}
                <tr class=d1> 
            {% else %}
                <tr class=d0>
            {% endif %}

            <td><a href="/jobs/{{ myJob.id }}/">{{ myJob.id }}</td><td>{{ myJob.duty }}</td> 
            <td>{{ myJob.updated }}</td><td>{{ myJob.priority }}</td>
            <td class=middle>{{ myJob.description }}</td>
            </tr>
        {% endif %}
    {% endfor %}
    </ul>
{% else %}
    <p>No jobs are in the system.</p>
{% endif %}
{%if作业列表%}
作业ID DUTYUPDATEDPRIORITYDESCRIPTION
{作业列表%中myJob的%1}
{%if myJob.priority而不是myJob.closeDate%}
{%if myJob.toggle%}
{%else%}
{%endif%}
{{myJob.id}{{myJob.duty}}
{{myJob.updated}{{myJob.priority}}
{{myJob.description}}
{%endif%}
{%endfor%}

{%else%}
系统中没有作业

{%endif%}
如果这是最后一次通过循环,则使用forloop.last-True:

{% if forloop.last %}
{% endif %}

感谢Stefanw,这正是我想要做的,我没有考虑迭代列表,因为在
len(data)==1
的情况下(是的,我以前写的不是一个好的python语句)我必须以不同的方式显示列表输出。无论如何,现在似乎一切都正常,再次感谢您的帮助!这绝对是正确的方法,但这就是我如何解决“没有变量作为属性名、字典键或列表索引”的问题问题。这确实不雅观,但它只使用内置标签和过滤器。我真的无法想象这是最好的解决方案。这对我的情况是最好的答案。我希望我有一个双重按钮