且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用`With`标签反转Django模板中的布尔值?

更新时间:2023-10-26 09:29:16

不知道这是否是***的解决方案,但我只是做了一个新的过滤器:

Not sure if this is the best solution, but I just made a new filter:

from django import template

register = template.Library()


@register.filter(name="not_value")
def not_value(true_value):
    return not true_value

然后做了:

{% load not_value %}
{% with s_options=disp|not_value %}  {# WILL NOT WORK WITH "as" #}
    {% include "e.html" with show_options=s_options only %}
{% endwith %}

请注意,po可以这样做(尽管我还没有尝试过):

Note that, possibly, this might work as well (though I have not tried):

{% include "e.html" with show_options=s_options|not_value only %}