2

In my django view, I have a list that looks like the above (except 50 lists embedded within the big list. In my template, how do I use a for loop to reference to display each of the elements within every list?

I am also trying to access a particular element within a list, for example the element 'c' in the below example. I tried feed[0][1] in the template but received an error.

feed = [ [0,a,b,c], [1,d,e,f], ... ] {% for video in feed %} #not sure what to put here {% endfor %} 

2 Answers 2

3

Something like this:

{% for video in feed %} {%for item in video %} {{item}} {% comment %} render it appropriately {% endcomment %} {%endfor%} {% endfor %} 

video is again a list so you can again iterate over it to get item in it and use it to render appropriate html.

EDIT: With reference to comment by #jdi and updated question, if you want to access particular element of list you can do:

{% video in feed %} {{ video.3 }} {% comment %} To access 3rd element {% endcomment %} {{ video|last }} {% comment %} To access last element {% endcomment %} {%endfor%} 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! Do you know how I can pick a specific element within the list? For example, how would I just pick the character 'c' in the above list? I tried doing item[3] but didn't work.
I will vote up this answer, but I think the {comment} might get confused for bad code since thats not a valid HTML comment. You should just remove it or comment with <!-- comment --> or the django comment tag {% comment %} comment {% endcomment %}
@sharataka: Instead of doing the second for loop, you would just access it by index: {{ video.3 }} or {{ video|last }}
@jdi, {# ... #} is django template syntax for single line comment. I agree it doesn't look good in the SO code syntax.
@Rohan: Dang it I over looked that variation. I apologize. You can revert my changes. I still voted you up
|
1

I use

 {%for D in TablePivot %} <tr class="{% cycle row1,row2 %}"> {%for valor in D %} <td>{{valor}}</td> {%endfor%} </tr> {%endfor%} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.