Django模板中是否可以使用aList中的元素? [英] Is 'if element in aList' possible with Django templates?

查看:144
本文介绍了Django模板中是否可以使用aList中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果[a,b,c]中的a,就像python

Does something like the python

if "a" in ["a", "b", "c"]:
    pass

存在于Django模板中?

exist in Django templates?

如果没有,是否有简单的方法来实现?

If not, is there an easy way to implement it?

推荐答案

p>这是您通常在查看函数中执行的操作。

This is something you usually do in your view functions.

aList = ["a", "b", "c"]
listAndFlags = [ (item,item in aList) for item in someQuerySet ]

现在您有一个简单的双元素列表,您可以显示

Now you have a simple two-element list that you can display

{% for item, flag in someList %}
    <tr><td class="{{flag}}">{{item}}</td></tr>
{% endfor %}

这篇关于Django模板中是否可以使用aList中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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