我按照类似线程中的说明进行操作,例如:How do you index on a jinja template?
但是我的 html 模板无法正常工作,因此整个 django 项目没有响应。
我收到的错误:
Error during template rendering.
Could not parse the remainder: '[loop.index0]' from 'songs_titles[loop.index0]'
我的代码如下所示:
{% 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 %}
并且 {{ongs_titles }} 呈现为列表,所以至少到这里为止它都可以正常工作。 我做错了什么?
最佳答案
实际上你正在寻找 Jinja,这在 django 上不起作用。
在 django 模板标签中,您应该使用 forloop.counter0
并且列表索引看起来像
{{songs_titles.1}}
需要在变量中设置计数然后使用它,为了设置变量你可以使用 -
{% with index=forloop.counter0 %}
{{ songs_titles.index}}
{% endwith %}
如果您有任何疑问,可以评论。
关于python - 无法解析余数 : '{{ list[loop.index0] }}' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683831/