python - Django 模板可能出现 'if element in aList' 吗?

标签 python django django-templates

做类似Python的事情

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

存在于 Django 模板中吗?

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

最佳答案

这是您通常在 View 函数中执行的操作。

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

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

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

关于python - Django 模板可能出现 'if element in aList' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1106849/

相关文章:

python - Django 管理员 : Creating users in the browser

python - Visual Studio Code - 即使安装了 Intellisense,它也无法在 SSH 服务器上运行

python - 良好的声音处理/分析/捕获模块

python - 用于 Python 的通用 REST/RPC API(等效于 OpenCPU)

python - Scrapy 没有为不同的基本 URL 正确生成输出?

django - 自定义标签在开始和结束时做一些事情

django - 无法通过生产中的邮件拦截404错误(debug = False)

python - django 反序列化尝试将输入编码为 ascii?

python - Django TemplateSyntaxError 无法解析余数 : '()'

html - Django REST 可浏览 API 模板更改