我将以下字典传递给渲染函数,其中 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 }} %}
以红色突出显示。
最佳答案
您不应该在 if
或 ifequal
语句中使用双括号 {{ }}
语法,您可以简单地访问变量就像在普通 python 中一样:
{% if title == source %}
...
{% endif %}
关于django - 如何在 Django 模板上使用 if/else 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372177/