{{ 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
<https://stackoverflow.com/questions/4651172/reference-list-item-by-index-within-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
<https://stackoverflow.com/users/5313669/bakuutin> and WeizhongTu
<https://stackoverflow.com/users/2714931/weizhongtu> for this specific
example:
from django import template
register = template.Library()
@register.filterdef 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 %}
On Wednesday, 9 October 2019 10:16:18 UTC+2, Luca Bertolotti wrote:
>
> 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
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2a47b2ff-5fc6-4c1d-91f1-79549d217ff0%40googlegroups.com.