[Answer]-How to achieve list Iteration in django template

1👍

Django templates will not allow you to do this. I’m not going to lecture you on keeping your logic out of your templates, because I think it’s a stylistic choice. But understand that this is the easiest way. If you need to use the index, you can access it as a forloop property, as explained in the documentation.

If you really want variable indexing, you could make your own custom template tag to do it. But, in this case, I suggest you use a more powerful templating language, like Jinja2, instead of torturing the Django templating language.

👤acjay

0👍

Django Template Language provides you a way to do this…

{% for item in listModels %} 
 {{ forloop.counter }} 

{% endfor %}

0👍

I fixed like I did the following:

{{ listModelsData|lookup:i|lookAttribute:"author" }} -// this equal listModelsData[i].author - this code in python 

@register.filter
def lookAttribute (d, token):
     return getattr (d, token)

Leave a comment