using django template lanuage to realize greater and less

时间秒杀一切 提交于 2021-02-11 15:15:53

问题


papis

i have a dict here

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

i want show it on my page so i using template as follows:

    {%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%}

as you see,i want the last column turn red text if its value is greater than 5

how can i realize this ,i google and found nothing

thanks all bro.

when i did as Daniel told:

  {%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%}

it told me the errors:

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'

what is the problem? thanks again crafet


回答1:


{% if forloop.last and v > 5 %}

Edit You need some spaces. Do it exactly as I have above, and it parses fine.

Generally, your code would be better - and more readable - if you followed PEP8 style for spaces around operators.



来源:https://stackoverflow.com/questions/6818099/using-django-template-lanuage-to-realize-greater-and-less

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!