sin*_*inθ 1 python django django-templates django-views
我想将一个值传递给一个include标签,该标签是传入的变量的 OPPOSITE。
这是我尝试过的(基本上):
{% with s_options as not disp %}
{% include "e.html" with show_options=s_options only %}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以做我想做的事吗?
不确定这是否是最佳解决方案,但我刚刚制作了一个新过滤器:
from django import template
register = template.Library()
@register.filter(name="not_value")
def not_value(true_value):
return not true_value
Run Code Online (Sandbox Code Playgroud)
然后做了:
{% load not_value %}
{% with s_options=disp|not_value %} {# WILL NOT WORK WITH "as" #}
{% include "e.html" with show_options=s_options only %}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)
请注意,这可能也有效(尽管我还没有尝试过):
{% include "e.html" with show_options=s_options|not_value only %}
Run Code Online (Sandbox Code Playgroud)
这是使用内置过滤器yesno 的一点技巧,但它有效:
{% with reversed_value=original_value|yesno:',True' %}
{# Do something #}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)
注意前面的逗号'True'。它的工作方式是,yesno如果原始值为,则将返回一个空字符串True,如果您对其进行任何布尔运算,它将评估为 False。