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

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

问题描述

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

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

在HTML模板中,我想完成以下几行:

  {%for source来源%} 
< tr>
< td> {{source}}< / td>
< td>
{%if title == {{source}}%}
刚刚!
{%endif%}
< / td>
< / tr>
{%endfor%}

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

  / admin / start / 
中的TemplateSyntaxError无法解析余数:'{{'from'{{'

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

解决方案

您不应该使用双括号 {{}} 语法 if ifequal 语句,你可以只需访问变量,就像在正常的python中一样:

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


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})

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 '{{'

...with {% if title == {{ source }} %} being highlighted in red.

解决方案

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天全站免登陆