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?

3
  • 1
    Please create an minimal reproducible example. What is branch_info? You can't use python inside of a django template Commented Jan 24, 2017 at 9:46
  • Related: Using index from iterated list Commented Jan 24, 2017 at 9:47
  • can you share the content in repo_info var? Commented Jan 24, 2017 at 9:54

2 Answers 2

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 %}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.