0

I have a list with a nested list returned from my view, something like -

[[1, [1, 2, 3]], [2, [2, 3, 4]], [3, [3,4,5]]] 

I want something like this, which would work in python -

for obj in my_list: for nested_obj in obj[1]: print nested_obj 

But with the django template system, if I try -

{% for obj in data_list %} <h2>{{obj.0}}</h2> <p> {{for nested_obj in obj.1}} <h5>{{nested_obj}}</h5> {{ endfor }} </p> {% endfor %} 

I get -

Could not parse the remainder: ' nested_obj in obj.1' from 'for nested_obj in obj.1' 

Why is this? Thanks!

Edit - So, that was stupid - I wrote {{for .... }} instead of {% for ... %} Thanks @allcaps

2 Answers 2

1

{{ for x in ... }} is causing a TemplateSyntaxError and should be {% for x in ... %}.

python manage.py shell

from django.template import Template, Context data_list = [[1, [1, 2, 3]], [2, [2, 3, 4]], [3, [3, 4, 5]]] template = """ {% for obj in data_list %} Obj {{obj.0}} {% for nested_obj in obj.1 %} Nested {{nested_obj}} {% endfor %} {% endfor %} """ t = Template(template) c = Context({"data_list": data_list}) print t.render(c) 

Out:

 Obj 1 Nested 1 Nested 2 Nested 3 Obj 2 Nested 2 Nested 3 Nested 4 Obj 3 Nested 3 Nested 4 Nested 5 
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this is working...

data_list = [[1, [1, 2, 3]], [2, [2, 3, 4]], [3, [3,4,5]]] {% for header, remainder in data_list %} <h2>a</h2> <p> {{ for x in remainder }} <h5>{{ x }}</h5> {{ endfor }} </p> {% endfor %} 

4 Comments

TemplateSyntaxError: Could not parse the remainder: ' x in remainder' from 'for x in remainder'
THen, how do you send the data_list to your template? are you sure it is in the form of a data_list ?
{{ for x in ... }} is causing a TemplateSyntaxError. {% for x in ... %} does not.
oh, geez, of course. Didn't notice that you were'nt using the '{% %}' syntax

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.