【发布时间】:2012-07-04 11:43:21
【问题描述】:
如何在 Django 模板中查询多字段?
例如,这个 if 语句不起作用(我知道我不能在 Django 模板中调用带参数的函数),但这表明了我想做的事情:
模板.html
{% for post in posts %}
{% if post.likes.filter(user=user) %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
models.py
class User(Model):
# fields
class Post(Model):
likes = ManyToManyField(User)
【问题讨论】:
-
标准django模板系统不允许调用带参数的方法。如果想调用上面的代码,你可以使用 Jinja2。
标签: python django django-templates