gpt4 book ai didi

django - 如何在 Django 模板上使用 if/else 条件?

转载 作者:行者123 更新时间:2023-11-28 19:36:05 25 4
gpt4 key购买 nike

我将以下字典传递给渲染函数,其中 sources 是一个字符串列表,title 是一个可能等于 sources 中的字符串之一的字符串:

{'title':title, 'sources':sources})

在 HTML 模板中,我想完成以下内容:

{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == {{ source }} %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}

但是,以下文本 block 会导致错误:

TemplateSyntaxError at /admin/start/
Could not parse the remainder: '{{' from '{{'

...{% if title == {{ source }} %} 以红色突出显示。

最佳答案

您不应该在 ififequal 语句中使用双括号 {{ }} 语法,您可以简单地访问变量就像在普通 python 中一样:

{% if title == source %}
...
{% endif %}

关于django - 如何在 Django 模板上使用 if/else 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372177/

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