python - 无法解析余数 : '{{ list[loop.index0] }}'

标签 python html django templates jinja2

我按照类似线程中的说明进行操作,例如: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/

相关文章:

python - Django : Connection refused to live_server_url

python - 具有多个应用程序的 Django 并重用相同的数据库表

python - 用于私有(private)文件传输的 bittorrent 跟踪器服务器 - python

python - 如何使用 pymongo 获取仅包含 ObjectId 的列表?

python - 如何在 whoosh 中使用 n-gram

python - ImportError : No module named django. session

python - 使用 python 降低音频剪辑的音量时出错

html - 媒体查询的绝对定位

php - 从查询中得不到结果

html - 如何使带标签的单选按钮居中