1

I followed instructions in simmilar thread like: How do you index on a jinja template?

but my html template is not working and whole django project is not responding due to this.

Error that I'm getting:

Error during template rendering.

Could not parse the remainder: '[loop.index0]' from 'songs_titles[loop.index0]'

My code looks like this:

{% if converted_files_urls %}
    <p>Titles: {{ songs_titles }}</p>
    {% for n in converted_files_urls %}
        <a href="{{ n }}" download>Download:  {{ songs_titles[loop.index0] }}</a>
        <br/>
    {% endfor %}
{% endif %}

and the {{ songs_titles }} renders as list, so at least till here it works ok. What am I doing wrong?

tikej
  • 179
  • 1
  • 16

2 Answers2

1

Actually you are looking for Jinja, that will not work on django.

In django template tag you should use forloop.counter0 and list indexing looks like

{{songs_titles.1}}

Need to set count in variable and then use it, for setting variable you could use -

{% with index=forloop.counter0 %}     

   {{ songs_titles.index}}

{% endwith %}

Still If you have any doubts you can comment it.

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
  • {{ songs_titles.1 }} works, and {{ forloop.counter0 }} shows 0, 1, 3, 4 and so on, but {{ songs_titles.forloop.counter0 }} doesn't show anything – tikej Oct 07 '18 at 09:07
  • Your solutions renders error :/ Error during template rendering. 'with' expected at least one variable assignment – tikej Oct 08 '18 at 10:44
  • Remove before and after space of "=", see the answer. – Pankaj Sharma Oct 08 '18 at 10:54
0

I finally resolved this by creating a custom template tag like here: https://djangosnippets.org/snippets/2740/

But to be honest it sucks that that's the simplest working solution for now :/

tikej
  • 179
  • 1
  • 16