Python django 模板中是否可以使用布尔逻辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4711627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:09:11  来源:igfitidea点击:

Is boolean logic possible in django templates?

pythondjangotemplatesboolean

提问by willcritchlow

I want to do something like:

我想做类似的事情:

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

Is that possible?

那可能吗?

采纳答案by Spacedman

Django docs on boolean operators

关于布尔运算符的 Django 文档

Gives you:

给你:

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

and

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

Be aware that andhas a higher order of precedence than or, and that parentheses are not possible. If required use nested blocks.

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