gpt4 book ai didi

django - 在 Django 模板中打印字典值

转载 作者:行者123 更新时间:2023-12-04 01:13:54 24 4
gpt4 key购买 nike

我正在传递两个名为 student_ids 的字典和 rows到 Django 模板。字典student_ids有一个名为 student_id 的 key 和 invite_id我想打印出 student_ids[student_id]获取 invite_id .

行字典中行中的第 4 个元素与 student_id 相同.当我尝试下面的代码时,它不起作用......

Django 模板:

{% for row in rows %}
<a href="#invite"
data-invite="{{ invite_ids.row.4 }}">
{{ row.2 }}
</a>
{% endfor %}

我应该怎么做才能在 Django 模板中获取正确的数据?我基本上需要 student.ids[row[4]]被打印出来,为了它很简单......

最佳答案

The dictionary student_ids has a key called student_id and invite_id and I would like to print out the student_ids[student_id] to get the invite_id.



尝试这个:
{% for sid, iid in student.ids.items %}
{{ sid }} -- {{ iid }}
{% endfor %}

顺便说一句:由于那个点,您的字典名称很奇怪且令人困惑。我会把它改成 studentIds或者其他的东西 ...

根据您的编辑更新:

首先检查这个: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

我真的不明白你到底在追求什么,但是如果你想根据一个等于 student_id 的键从行字典中访问一个值你可以试试这个,看看它是如何为你工作的,并根据你的需要进行调整:
{% for student_id, student_id_value in student.ids.items %}
{{ student_id }}
{{ rows.student_id }}
{% endfor %}

关于django - 在 Django 模板中打印字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12173245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com