Python Django'if and'模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14957564/
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 12:57:24  来源:igfitidea点击:

Django 'if and' template

pythondjango

提问by user1646528

I want to do the following in my django html page:

我想在我的 django html 页面中执行以下操作:

{% if myList|length and ifequal myValue 'somestring' %}
blah blah
{% endif %}

but I get the error: Unused 'myValue ' at end of if expression

但我收到错误: 在 if 表达式末尾未使用 'myValue'

How can I do an If AND in a template??

如何在模板中执行 If AND ?

回答by pcalcao

I think you might have to separate the ifand the ifequalin two statements:

我认为您可能需要在两个语句中将 theif和 the分开ifequal

{% if myList|length  %}
     {% ifequal myValue 'somestring' %}
          blah blah
     {% endifequal %}
{% endif %}

回答by kmerenkov

Should be something like this:

应该是这样的:

{% if myList|length and myValue == 'somestring' %}
   blah blah
{% endif %}

回答by arulmr

Try this:

尝试这个:

{% if myList|length and myValue == 'somestring' %}
    blah blah
{% endif %}

Refer django documentation about use of boolean-operatorsand complex-expressionsin django templates.

请参阅有关在 django 模板中使用布尔运算符复杂表达式的 django 文档。

回答by Mr Singh

Django docs on boolean operators

关于布尔运算符的 Django 文档

Should be something like this

  1. view.py

应该是这样的

  1. 查看.py
def posts_request(request):  
    message=ApplyPost.objects.filter( blah blah)
    if message:
       return render (request,'blah.html',{'message':message})
  1. blah.html
  1. 等等.html
{% if message %}
     {% for post in message %}
          {% if post.provider %}
              ....
          {% endif %}
          {% if post.accept == True and post.provider %}
              ...
              ...
          {% endif %}
          ....You can add multiple if,elif, with Operators ....
    {% endfor %}
{% if message %}

and

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

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

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