passing a list of list to a template

33 views
Skip to first unread message

dk

unread,
Feb 12, 2015, 5:55:25 PM2/12/15
to django...@googlegroups.com
i do have a list  of list like this
[  [apple, banana, red] ,  [orange, grape, blue] ,  [watermelon, tangerine, purple]       ]


then i am passing it to the template like
return render(request, "show_table.html", {"lista": lista})
inside my template html i have


    <table border="1" style="width: 100%">
{% for i in lista %}
<tr>
<td>{{ i }}</td>
{# <td> ----> Temp </td>#}
{# <td> {{ i }}</td>#}
</tr>

{% endfor %}
</table>


but how can i select i[0]   or i[2] to put then into a diferent column?    might be a better trick on how to send it?
thanks.

Vijay Khemlani

unread,
Feb 12, 2015, 6:15:50 PM2/12/15
to django...@googlegroups.com
If you have a fixed number of items in each of the sublists you can do

{{ i.0 }}   # First element
{{ i.1 }}   # Second element

or you can iterate over it

{% for sub_element in i %}
    {{ sub_element }}
{% endfor %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec4f230b-73f0-495c-ad2d-db921ed227cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

monoBOT

unread,
Feb 12, 2015, 6:30:04 PM2/12/15
to django...@googlegroups.com
{% for x, y in points %}
    There is a point at {{ x }},{{ y }}
{% endfor %}
extracted from:
https://docs.djangoproject.com/en/1.7/ref/templates/builtins/



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec4f230b-73f0-495c-ad2d-db921ed227cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

ADEWALE ADISA

unread,
Feb 12, 2015, 6:39:58 PM2/12/15
to django...@googlegroups.com

What of:

  <table border="1" style="width: 100%">
    {% for i in lista %}
        <tr>

           {% for j in i %}
               <td>{{ j }}</td>
           {% endfor %}
        </tr>

    {% endfor %}
    </table>

dk

unread,
Feb 13, 2015, 9:14:37 PM2/13/15
to django...@googlegroups.com
I am assuming in django  doing i.1  or i.2  will be the same as  i[1],  i[2] right?

can I do if statements too?  like if i.2 == to somestuff? do something? or all that need to be set in the view function?

thanks

Vijay Khemlani

unread,
Feb 13, 2015, 10:45:29 PM2/13/15
to django...@googlegroups.com
I think you can make comparisons, but try to limit the amount of logic that you implement in your templates.

Adailton (Dhelbegor)

unread,
Feb 14, 2015, 10:37:55 AM2/14/15
to django...@googlegroups.com
#views
def tests(request):
    acept
= get_object_or_404(Modeltest, released=True)
    context
= {'keys': acept} # [1]
   
return render(request, 'temp_test.html', context)


#template
{% for key in keys %} # [1]
     
{{key.title}}
     
{{key.name}}
     
{{key.number}}
     
{{key.etc}}
{% endfor %}

Reply all
Reply to author
Forward
0 new messages