是否可以在django模板中检查对象是否包含在列表中

时间:2021-07-28 20:19:37

I'm very new to django, about a week into it.

我对django很新,大约一个星期。

I'm making a site where users enter stuff, then other users can vote on whether they like the stuff or not. I know it's not so novel, but it's a good project to learn a bunch of tools.

我正在创建一个用户输入内容的网站,然后其他用户可以投票决定他们是否喜欢这些内容。我知道它不是那么新颖,但是学习一堆工具是一个很好的项目。

I have a many-to-many table for storing who likes or dislikes what. Before I render the page, I pull out all the likes and dislikes for the current user, along with the stuff I'm going to show on the page.

我有一个多对多的表来存储谁喜欢或不喜欢什么。在我呈现页面之前,我将为当前用户提取所有喜欢和不喜欢的内容,以及我将要在页面上显示的内容。

When I render the page, I go through the list of stuff I'm going to show and print them out one at a time. I want to show the user which stuff they liked, and which they didn't.

当我渲染页面时,我会查看我要显示的内容列表,并逐个打印出来。我想向用户展示他们喜欢的东西,以及他们没有的东西。

So in my django template, I have an object called entry. I also have two lists of objects called likes and dislikes. Is there any way to determine if entry is a member of either list, inside my django template.

所以在我的django模板中,我有一个名为entry的对象。我还有两个名为“好恶”的对象列表。有没有办法在我的django模板中确定条目是否是任何一个列表的成员。

I think what I'm looking for is a filter where I can say something like

我认为我正在寻找的是一个过滤器,我可以这么说

{% if entry|in:likes %}

or

{% if likes|contains:entry %}

I know I could add a method to my model and check for each entry individually, but that seems like it would be database intensive.

我知道我可以为我的模型添加一个方法并单独检查每个条目,但这似乎是数据库密集型的。

Is there a better way to think about this problem?

有没有更好的方法来考虑这个问题?

3 个解决方案

#1


0  

Go here. Very similar to what they're using on trunk. "Save this as smart_if.py in the templatetags folder of one of your apps. Then a simple {% load smart_if %} replaces the boring built-in Django {% if %} template with the new smart one."

到这里。与他们在行李箱上使用的非常相似。 “将它保存为smart_if.py在你的某个应用程序的templatetags文件夹中。然后一个简单的{%load smart_if%}用新的智能模板取代了无聊的内置Django {%if%}模板。”

#2


13  

If you're using latest django version, then it's just

如果您使用的是最新的django版本,那么它就是

{% if entry in likes %}

Refer django docs

请参阅django docs

#3


-1  

If you're not running trunk one of the following should work:

如果您没有运行主干,则应执行以下操作之一:

Filter:

Replacement "if" tag, largely the basis for the new functionality in the upcoming 1.2 release:

替换“if”标签,主要是即将发布的1.2版本中新功能的基础:

#1


0  

Go here. Very similar to what they're using on trunk. "Save this as smart_if.py in the templatetags folder of one of your apps. Then a simple {% load smart_if %} replaces the boring built-in Django {% if %} template with the new smart one."

到这里。与他们在行李箱上使用的非常相似。 “将它保存为smart_if.py在你的某个应用程序的templatetags文件夹中。然后一个简单的{%load smart_if%}用新的智能模板取代了无聊的内置Django {%if%}模板。”

#2


13  

If you're using latest django version, then it's just

如果您使用的是最新的django版本,那么它就是

{% if entry in likes %}

Refer django docs

请参阅django docs

#3


-1  

If you're not running trunk one of the following should work:

如果您没有运行主干,则应执行以下操作之一:

Filter:

Replacement "if" tag, largely the basis for the new functionality in the upcoming 1.2 release:

替换“if”标签,主要是即将发布的1.2版本中新功能的基础: