在模板中使用模板标签返回值作为条件逻辑

问题描述 投票:0回答:1

我有一个返回True或False的模板标记。例如:

@register.simple_tag
def is_home(context):
   if homepage:
       return True
   else:
       return False

我想使用此标记来更改模板中的图标:

{% if is_home %}
  <svg data-src="{% static 'images/cart_white.svg' %}" width="35" height="30"/>
{% else %}
   <svg data-src="{% static 'images/cart.svg' %}" width="35" height="30"/>
{% endif %}

但是模板标签不会像这样调用。

只有当我这样称呼它:{%is_home%}它才有效但我不能将结果用于条件。

任何想法如何将结果用于逻辑?

python django django-templates
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.