【问题标题】:Is boolean logic possible in django templates?django 模板中是否可以使用布尔逻辑?
【发布时间】:2011-06-10 08:15:06
【问题描述】:

我想做这样的事情:

{% if ("view_video" in video_perms) OR purchase_override %}

这可能吗?

【问题讨论】:

  • 在某些时候,您应该为此使用模型和视图函数。为什么这不是模型对象的属性?还是登录用户的属性?

标签: python django templates boolean


【解决方案1】:

Django docs on boolean operators

给你:

{% if user in users %}
  If users is a QuerySet, this will appear if user is an
  instance that belongs to the QuerySet.
{% endif %}

{% if a == b or c == d and e %}

请注意and 的优先级高于or,并且不能使用括号。如果需要,请使用嵌套块。

【讨论】:

  • 现在好了。那将是明智的选择。我确实在那里尝试过,但在我的手机上,我找不到我需要的位。谢谢您的帮助。抱歉这个菜鸟问题。
  • +1 用于最后的反手链接评论。先生打得很好。
  • @will - 可能值得注意的是,更复杂的 if 构造只能在 Django 1.2 及更高版本中使用(参见 the release notes
  • @willcritchlow:“在我的手机上”?您正在使用手机构建 Django 应用程序吗?严重地?您应该考虑购买带键盘和显示屏的电脑,这可能比在手机上更容易。
  • 嘿。我怀疑他只是想在远离电脑的情况下在手机上查找文档。我有时会这样做。灵感来袭,在手机上谷歌一下,收藏任何有用的东西。
猜你喜欢
  • 2013-01-24
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2011-05-13
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多