【问题标题】:using django template lanuage to realize greater and less使用django模板语言实现great and less
【发布时间】:2011-07-25 14:59:54
【问题描述】:

爸爸

我这里有一个字典

dict = {1: [1,2,3,4,5,6,7,8,9],
        2: [2,4,5,6,7,8,9,0],
        3: [5,2,4,6,12,3,7,6]}

我想在我的页面上显示它 所以我使用模板如下:

    {%for item in dict.items%}
    <tr>
    <td>{{item.0}}</td>
    {%for v in item.1%}
    here ,i dont know how to handle
    if last column and v >5
    <td color = 'red'>{{v}}</td>
    else
    <td>{{v}}</td>
   {%endfor%}
</tr>
{%endfor%}

如您所见,如果最后一列的值大于 5,我希望最后一列变为红色文本

我怎么能意识到这一点,我谷歌却一无所获

谢谢大家。

当我按照丹尼尔所说的去做时:

  {%for item in dict.items%}
    <tr>
    <td>{{item.0}}</td>
    {%for v in item.1%}
   {%if forloop.last and v > 5%}
    <td color = 'red'>{{v}}</td>
    {%else%}
     <td >{{v}}</td>
     {%endif%}
    else
    <td>{{v}}</td>
   {%endfor%}
</tr>
{%endfor%}

它告诉我错误:

Could not parse the remainder: '>5' from '>5'

Request Method:     GET
Request URL:    http://10.64.41.134:8000/monthlyinfo/
Django Version:     1.3
Exception Type:     TemplateSyntaxError
Exception Value:    

Could not parse the remainder: '>5' from '>5'

有什么问题? 再次感谢 手艺

【问题讨论】:

  • 新手说谢谢你编辑代码,Daniel

标签: django templates tags


【解决方案1】:
{% if forloop.last and v > 5 %}

编辑您需要一些空格。完全按照我上面的方法去做,它解析得很好。

通常,如果您在运算符周围的空格遵循PEP8 样式,您的代码会更好 - 并且更具可读性。

【讨论】:

  • Daniel,我在这里被告知错误:请求方法:GET 请求 URL:10.64.41.134:8000/monthlyinfo Django 版本:1.3 异常类型:TemplateSyntaxError 异常值:无法解析剩余部分:'>4.5' from '> 4.5' 异常位置:C:\Python26\lib\site-packages\django\template\base.py in init,第 502 行 Python 可执行文件:C:\Python26\python.exe Python 版本:2.6 .7
猜你喜欢
  • 2012-11-29
  • 1970-01-01
  • 2019-08-01
  • 2011-03-29
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
相关资源
最近更新 更多