python - DJANGO - 在模板变量中使用 forloop.counter

标签 python html django templates

我目前的模板中有这个变量:

{{ product_list.0.image_set.all.0.image.url }}

它成功给出了产品列表中第一个产品中第一张图片的 URL。

我想要这样的东西:

{{ product_list.forloop.counter.image_set.all.0.image.url }}

这样我就可以在for循环中获取product_list中每个图像的第一张图像。但上面的代码不起作用。这可能吗?

最佳答案

不要使用forloop.counter,逐个对象地迭代product_list:

{% for product in product_list %}
    {{ product.image_set.all.0.image.url }}
{% endfor %}

关于python - DJANGO - 在模板变量中使用 forloop.counter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004831/

相关文章:

python - 如何使用 Python 正确提供 json 数据?

javascript - 使用 Javascript 确定文件是否存在(本地)

python - django.db.utils.IntegrityError : NOT NULL constraint failed: unesco_site. 类别_id

django - 需要澄清如何使用 Django 1.4 表单向导,特别是预填写和保存

django - 如何在 django 中为 HttpRequest.GET 设置默认值?

python - 密集层权重形状

python - 社会安全号码检查 - Python

python - TensorFlow 无法识别 feed_dict 输入

html - 用均匀的空间证明 Bootstrap navbar-nav 列表项

javascript - 拖动滚动包含 float div 的 div