且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查 Django 模板中的列表变量项中是否存在某些内容

更新时间:2023-11-25 22:52:46

理想情况下,您要做的是创建一个模板获取的列表:

Ideally what you would do is create a list that the template gets as such:

l = [s.name for s in sections]

在模板中,使用:

{% if 'Social' in l %}

您正试图在模板中加入比预期更多的逻辑.模板应该尽量少用逻辑,而逻辑应该在填充模板的代码中.

You're trying to put more logic into a template than they are meant to have. Templates should use as little logic as possible, while the logic should be in the code that fills the template.