Django -- {% if %} 块中的模板标签 [英] Django -- Template tag in {% if %} block
问题描述
我将以下字典传递给渲染函数,源是字符串列表,标题是可能等于源中的字符串之一的字符串:
I have the following dictionary passed to a render function, with sources being a list of strings and title being a string potentially equal to one of the strings in sources:
{'title':title, 'sources':sources})
在 HTML 模板中,我想完成以下几行内容:
In the HTML template I'd like to accomplish something among the lines of the following:
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == {{ source }} %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}
但是,以下文本块会导致错误:
However, the following block of text results in an error:
TemplateSyntaxError at /admin/start/
Could not parse the remainder: '{{' from '{{'
...{% if title == {{ source }} %}
以红色突出显示.
...with {% if title == {{ source }} %}
being highlighted in red.
推荐答案
你不应该在 if
或 中使用双括号
语句,您可以像在普通 python 中一样简单地访问那里的变量:{{ }}
语法>ifequal
You shouldn't use the double-bracket {{ }}
syntax within if
or ifequal
statements, you can simply access the variable there like you would in normal python:
{% if title == source %}
...
{% endif %}
这篇关于Django -- {% if %} 块中的模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!