Django -- {% if %} 块中的模板标签 [英] Django -- Template tag in {% if %} block

查看:35
本文介绍了Django -- {% if %} 块中的模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 中使用双括号 {{ }} 语法>ifequal 语句,您可以像在普通 python 中一样简单地访问那里的变量:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆