list cycle in template

23 views
Skip to first unread message

Luca Bertolotti

unread,
Oct 9, 2019, 4:16:18 AM10/9/19
to Django users
Hello in the view a hve a list
mylist = ['aa','bb']
n = range(len(mylist))

return render(....{'mylist':mylist,'n':n,.....})

in the template i do this:

<tr>{% for a in n %} <td>{{ mylist.a }}</td></tr>

it never show nothing, but if i do:

<tr>{% for a in n %} <td>{{ mylist.0 }}</td></tr>
it show 'aa'

Where is the mistake?

Thanks

Cornelis Poppema

unread,
Oct 9, 2019, 9:30:45 AM10/9/19
to Django users
{{ mylist.a }} means: get attribute "a" from object "mylist". For the template it doesn't matter if you have defind a variable "a", it will get the literal "a". Also see https://stackoverflow.com/questions/4651172/reference-list-item-by-index-withdoesin-django-template for others with the same problem and a solution. You can add a template filter which does support variables. Your template will then look like {{ mylist|index:a }}. Credits to Bakuutin and WeizhongTu for this specific example:

from django import template
register = template.Library()

@register.filter
def index(indexable, i):
    return indexable[i]

{% load index %}
{{ mylist|index:a }}

I believe you will need to do the same if you want to access any property from that item, ie. {{ mylist|index:a }}.property doesn't work, you would need to write a new filter to be able to do {{ mylist|index:a|attr:"property" }}

Besides all this, is there a reason you cannot simply use a for-loop to iterate over mylist ?

{% for item in mylist %}
    {{ item.property }}
{% endfor %}

Luqman Shofuleji

unread,
Oct 9, 2019, 1:28:37 PM10/9/19
to django...@googlegroups.com
1) you forgot to include  {% endfor %} in you template

2) I believe in your view all you actually need is:
mylist = ['aa','bb'] 
return render(....{'mylist':mylist})  

Then in your template:
                      <tr>
                        {% for a in mylist %}
                          <td>{{ mylist }}</td>
                        {% endfor %}
                      </tr>




 


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/af0a2286-6bf9-4b9e-9a77-626ae5309ee6%40googlegroups.com.

Luqman Shofuleji

unread,
Oct 9, 2019, 1:28:50 PM10/9/19
to django...@googlegroups.com
Sorry little mistake there:

Then in your template:
                      <tr>
                        {% for a in mylist %}
                          <td>{{ a }}</td>
                        {% endfor %}
                      </tr>

Luca Bertolotti

unread,
Oct 10, 2019, 2:43:22 AM10/10/19
to Django users
Hello I have solved using a tuple because i need to compare value:

{% for matric,lotto,rimanenza in lista_finale %} {% if matric == dati.partn %}<td>{{ lotto }} -- {{ rimanenza }}</td>{% endif %}{% endfor %}</tr>

Where lista finale is a list of tuple

Thanks to all
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages