在Django模板中如何获取列表的索引?

huangapple go评论45阅读模式
英文:

How can I get index of list in django template

问题

I have 3 list item1, item2, item3

views.py
context = {'item0': region.objects.all(),
           'item1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           'item2': ['one', 'two', 'three', 'four', 'five'],
           'item3': ['six', 'seven', 'eight', 'nine', 'ten'],
           'item4': "test",
           'item5': 5,
           'item6': range(5),
           }
return render(request, 'btscreate.html', context)

I tried to get in the template:

{% for num in item1 %}
    <td> {{ num }} </td> # this works
    <td> {{ how can I get values item2 and item3 ? }} </td>
{% endfor %}
{% for num in item6 %}
    <td> {{ item2.0 }} </td> # that works
    <td> {{ item2.num }} </td> # but that doesn't work
{% endfor %}

how can I get values item2 and item3 ?

英文:

I have 3 list item1,item2,item3

views.py
context={&#39;item0&#39;: region.objects.all(),
         &#39;item1&#39;: [1,2,3,4,5,6,7,8,9,10],
         &#39;item2&#39;: [&#39;one&#39;, &#39;two&#39;,&#39;three&#39;,&#39;four&#39;, &#39;five&#39;],
         &#39;item3&#39;: [&#39;six&#39;, &#39;seven&#39;,&#39;eight&#39;,&#39;nine&#39;,&#39;ten&#39;],
         &#39;item4&#39;: &quot;test&quot;,
         &#39;item5&#39;: 5,
         &#39;item6&#39;: range(5),
         }
return render(request, &#39;btscreate.html&#39;, context )

I tried get in template

1)

{% for num in item1 %}
&lt;td&gt; {{ num }} &lt;/td&gt; # this work 
&lt;td&gt; {{ how can I get values item2 and item3 ? }} &lt;/td&gt;
{% endfor %}

2)

{% for num in item6 %}
&lt;td&gt; {{ item2.0 }} &lt;/td&gt; # that work 
&lt;td&gt; {{ item2.num }} &lt;/td&gt; # but that doesn&#39;t work
{% endfor %}

how can I get values item2 and item3 ?

答案1

得分: 1

你正在尝试的目标,据我所知,不可能在模板中实现。

为了确保我们理解一致,我将展示Python代码,你希望将其翻译成"Django模板语法"。

r = range(3)
list1 = ["ape", "whale", "giraffe"]
list2 = ["brown", "blue", "yellow"]
for idx in r:
    print(list1[idx], list2[idx])

解决方案1:无需使用for循环并访问每个项目。这可能非常繁琐,但仍然是一个选择。

<table>
  <tr>
    <td> {{ item2.1 }} </td>
    <td> {{ item3.1 }} </td>
  </tr>
  <tr>
    <td> {{ item2.2 }} </td>
    <td> {{ item3.2 }} </td>
  </tr>
</table>

解决方案2:正如我之前所说,无法在模板语法中同时迭代两个列表,就像我的Python示例中所示。你需要在视图中使用zip来合并这些列表:
views.py:

item2 = ['one', 'two', 'three', 'four', 'five']
item3 = ['six', 'seven', 'eight', 'nine', 'ten']
context={'item0': region.objects.all(),
         'item1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
         'item23': zip(item2, item3),
         'item4': "test",
         'item5': 5,
         'item6': range(5),
         }

然后在你的模板中可以这样使用:
temp.html:

<table>
{% for i2, i3 in item23 %}
<tr>
  <td> {{ i2 }} </td>
  <td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>

如果你的问题目标只是如何显示循环的索引,你可以这样做:
temp.html:

<table>
{% for i2, i3 in item23 %}
<tr>
  <td> {{ forloop.counter0 }} </td>
  <td> {{ i2 }} </td>
  <td> {{ i3 }} </td>
</tr>
{% endfor %}
</table>

你不能将这个forloop计数器用作上述我的Python示例中其他列表的索引。

英文:

What you are trying to achieve is, as far as I know, not possible within templates.

To make sure we are on the same page I will show the code in python that you want to translate to "django-template-syntax"

r = range(3)
list1 = [&quot;ape&quot;, &quot;whale&quot;, &quot;giraffe&quot;]
list2 = [&quot;brown&quot;, &quot;blue&quot;, &quot;yellow&quot;]
for idx in r:
    print(list1[idx], list2[idx]

Solution 1:
Do it without a for-loop and access every item. Might be very tedious but still an option.

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt; {{ item2.1 }} &lt;/td&gt;
    &lt;td&gt; {{ item3.1 }} &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt; {{ item2.2 }} &lt;/td&gt;
    &lt;td&gt; {{ item3.2 }} &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

Solution 2:
As I said iterating over two lists simultaneously like shown in my python example is not possible within the templating-syntax. You need to zip those lists inside your views:
views.py:

item2 =  [&#39;one&#39;, &#39;two&#39;,&#39;three&#39;,&#39;four&#39;, &#39;five&#39;]
item3 = [&#39;six&#39;, &#39;seven&#39;,&#39;eight&#39;,&#39;nine&#39;,&#39;ten&#39;]
context={&#39;item0&#39;: region.objects.all(),
         &#39;item1&#39;: [1,2,3,4,5,6,7,8,9,10],
         &#39;item23&#39;: zip(item2, item3)
         &#39;item4&#39;: &quot;test&quot;,
         &#39;item5&#39;: 5,
         &#39;item6&#39;: range(5),
         }

Then in your template you do it like this:
temp.html:

&lt;table&gt;
{% for i2, i3 in item23 %}
&lt;tr&gt;
  &lt;td&gt; {{ i2 }} &lt;/td&gt; 
  &lt;td&gt; {{ i3 }} &lt;/td&gt; 
&lt;/tr&gt;
{% endfor %}
&lt;/table&gt;

If your question's target is just how to display the index of the loop you can do like this:
temp.html:

&lt;table&gt;
{% for i2, i3 in item23 %}
&lt;tr&gt;
  &lt;td&gt; {{ forloop.counter0 }} &lt;/td&gt;
  &lt;td&gt; {{ i2 }} &lt;/td&gt; 
  &lt;td&gt; {{ i3 }} &lt;/td&gt; 
&lt;/tr&gt;
{% endfor %}
&lt;/table&gt;

You can not use that forloop counter as an index for other lists similar to my python example above.

huangapple
  • 本文由 发表于 2023年3月7日 19:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定