0
{% for repo in repo_info %}

{% for branch in branch_info[forloop.counter] %}
            <li>Branch Name --> {{ branch }}</li>           
{% endfor %}

{% endfor %}

branch_info is a list of lists.

It gives me error that could not parse the remainder on this ---> branch_info[forloop.counter]

Is there any way to parse over the list elements which are also a list?

Sayse
  • 42,633
  • 14
  • 77
  • 146
Luv33preet
  • 1,686
  • 7
  • 33
  • 66

2 Answers2

0

You can create a simple template tag that returns the data at the requested index

#  some file named my_template_tags.py
@register.simple_tag
def at_index(data, index):
    return data[index]

This will throw an exception if you use an invalid index. If you don't want an exception, you will have to catch it and return some valid data.

It can also be used with dictionaries but you pass in the key instead of the index.

{% load my_template_tags %}

{% for repo in repo_info %}

    {% for branch in branch_info|at_index:forloop.counter %}
            <li>Branch Name --> {{ branch }}</li>           
    {% endfor %}

{% endfor %}
Resley Rodrigues
  • 2,218
  • 17
  • 17
0

Most of the time when your template code start being messy like this it means your data don't have the right structure. In this case, it seems that you are relying on repo_info and branch_info being "parallel sequences" (data at branch_info[x] are for repo at repo_info[x]).

The cleanest solution would be for repo to hold it's own list of branch so you can just iterate over repo_info and then for each repo iterate over repo.branches.

If you cannot easily structure your data this way, another solution is to zip (or itertools.izip) repo_info and branch_info together in your view so you can iterate on (repo, branches) tuples in your template.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118