python - Iterating over nested list -


i have list nested list returned view, -

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

i want this, work in python -

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

but django template system, if try -

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

i -

could not parse remainder: ' nested_obj in obj.1' 'for nested_obj in obj.1' 

why this? thanks!

edit - so, stupid - wrote {{for .... }} instead of {% ... %} @allcaps

{{ x in ... }} causing templatesyntaxerror , should {% 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 = """     {% obj in data_list %}         obj {{obj.0}}         {% 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 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -