在循环期间将 Jinja2 用于唯一的 div ID?

Using Jinja2 for unqiue div ID's during loop?

我似乎想不出解决这个问题的方法..

我有一个模板,它从数据库中的几个不同 table 中获取输入。那些 table 包含 x 数量的信息。为了仅在 onClick 上显示一些信息,我使用了一些 javascript 来隐藏它包含的 div。但是,那个 div id={{ row.id }} 在 jinja 的 forloop 期间被填充。一开始我认为它运行良好,直到我意识到 {{ row.id }} 可以与不同的 {{ row.id }} 相同,如果它来自不同的 table。 . 例如;

{% for row in packages %} # Packages is one of the tables
<div id="{{ row.id }}">
{{ row.price }}
{{ row.date }}
..etc
</div>
<button onClick="toggleDiv("{{ row.id }}">Show Content</button>

other stuff in between...

{% for entry in services %} # Services is one of the tables
<div id="{{ entry.id }}"> # is it possible to do something like entry.id + {{ loop.index }} ? I'm not positive that would make it unique tho.
{{ entry.price }}
{{ entry.date }}
..etc
</div>
<button onClick="toggleDiv("{{ entry.id }}">Show Content</button>

所以我 运行 的问题是 row.id == entry.id 是可能的,因为它们来自不同的 table。我想弄清楚是否可以使用带有随机唯一数字的大列表来从中提取 div id?我不确定如何让 jinja 做到这一点?我在这里运气不好吗?这是此模板中的最后一个主要障碍,所以我很想找出一些解决方案。有什么想法吗?

为什么不直接在 html ID 前加上 table 名称?

{% for row in packages %} # Packages is one of the tables
<div id="package_{{ row.id }}">
{{ row.price }}
{{ row.date }}
..etc
</div>
<button onClick="toggleDiv("package_{{ row.id }}")">Show Content</button>


{% for entry in services %} # Services is one of the tables
<div id="service_{{ entry.id }}"> 
{{ entry.price }}
{{ entry.date }}
..etc
</div>
<button onClick="toggleDiv("service_{{ entry.id }}")">Show Content</button>