python - 如何在 django 模板中迭代多个列表
问题描述
我正在尝试在模板上迭代我的嵌套字典,但它不迭代卡片,但它返回这样的列表(第一个示例图像)而不是迭代卡片。
但想要迭代三个列表,如第二张图片(第二张示例图片) 也就是说,如果每张卡片都有相关的帖子,并且每个帖子都关联(列表)内容,
所以这是我的嵌套字典
cards = [
{'card_title': card_postings,
'posts': [
{'post_title': arrays,
'contents': post_content,}
],
},
]
这也是模板
{% for card in cards %}
<div class="card">
<div class="card-body">
<h4 class="text-center">{{ card.card_title }}</h4>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
{% for post in cards.posts %}
<div class="col">
<div class="card">
<div class="card-header text-center text-primary">
<h5>{{ post.post_title }}</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item border-0">
{% for content in post.contents %}
<i>{{ content }} <br> </i>
{% endfor %}
</li>
</ul>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
{% endfor %}
顺便说一句,嵌套字典中的所有这些变量(card_postings、arrays、post_content)都是列表
解决方案
推荐阅读
- android - Retrofit 中的 IllegalArgumentException / 不能有替换块 / Dynamic Url / Retrofit 2
- flutter - 如何从 Play 商店获取软件包名称和版本号?
- sql - 使用两个字段的条件在记录集上执行查找
- c# - 要列出的数据表
c#中没有for循环 - excel - 基于两列值的 VLOOKUP
- sql - 来自 2 个类似表的 2 个其他 SUM 的总和
- javascript - 如何增加椭圆的亮度
- javascript - 如何在OpenLayers中获取用户的当前位置以及经纬度
- c# - 我为我的 2D 游戏制作了动画,但我的角色没有移动
- wikipedia - 检索在 Wikipedia 或 Mediawiki 中有书目的人的所有文章 - 使用 Wikipedia API 或其他方式