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
Django 'if and' template
提问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.
回答by Mr Singh
Django docs on boolean operators
Should be something like this
- view.py
应该是这样的
- 查看.py
def posts_request(request):
message=ApplyPost.objects.filter( blah blah)
if message:
return render (request,'blah.html',{'message':message})
- blah.html
- 等等.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,并且括号是不可能的。如果需要,使用嵌套块

