Reputation: 3
I have a problem with my template.
I know how to use list.index to get a value from a list, but now my index value is list.container.id (=1)
.
So, I have stupidly try list.container.id
and is not working like list.1
.
Upvotes: 0
Views: 492
Reputation: 1204
Edit: As pointed out the old answer was wrong,
New Solution:
Create a custom filter tag.
In your app, create a python module called templatetags (create a folder and place an init.py file in it) then create a py file for your custom filter, for ex: my_custom_filter.py
from django import template
register = template.Library()
@register.filter
def index_my_list(my_list, index):
try:
return my_list[index]
except IndexError:
return None
Use in your template as
{{list|index_my_list:container.id}}
All this being said, it is best to keep your logical code in the view, you might want to reconsider your data structures
Wrong Solution:
Assign container.id to a variable then use it
{% with container.id as foo %}
{{list.foo}}
Upvotes: 1