how to start the forloop.counter from a different

2019-01-20 18:38发布

I have 2 seperate forloops and i am using forloop.counter in bothloops. I want to start the second forloop counter from the ending of first forloop

{% for i in something1 %}
  <tr>
    <td>{{ forloop.counter }}</td>
    <td>i.username</td>
  </tr>
{% endfor %}
{% for j in something2 %}
  <tr>
    <td>{{ forloop.counter }}</td>
    <td>j.username</td>
  </tr>
{% endfor %}

if the first forloop ends at 10 then i want to start the next for loop from 11.plz help

2条回答
The star\"
2楼-- · 2019-01-20 19:02

I'm not comfortable with Django, so I show a couple of option in plain Python, given the collections:

something1 = [1,2,3,4]
something2 = [1,2,3,4,5,6,7,8,9,10]

You can access objects by index (not the same as database index):

i = 1
for e1 in something1:
  print(e1)
  i += 1

for i2 in range(i,len(something2)):
  print(something2[i2])

Or slice the last collection:

for e1 in something1:
  print(e1)

for e2 in something2[len(something1):-1]:
  print(e2)

Of course, the last collection has to be the longest.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-20 19:19

Python's slicing features are quite extensive.
the syntax looks like that: SOME_STRING[start:stop:step].
So basically you can use it pretty much however you like.

I wanted to comment it on your comment, but unfortunately I don't have enough rep :)

查看更多
登录 后发表回答