【问题标题】:Django; How to do conditionals for custom templatetags姜戈;如何为自定义模板标签做条件
【发布时间】:2019-01-29 11:11:36
【问题描述】:

我一直在从事 Django 项目。似乎条件不适用于模板标签。我怎样才能让它发挥作用?

我的自定义模板标签是这样的。

register = template.Library()


@register.simple_tag(takes_context=True)
def get_counts(context):
    ...
    return counts

在模板中

{% load app_name_tags %}
...
{% if not get_counts == 0 %}
{% get_counts %}
{% endif %}

但是这个条件不起作用。我该如何解决这个问题?

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    试试这个,也许会奏效:

    {% get_counts as counts %}
    {% if not counts == 0 %}
    {{ counts }}
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2021-08-11
      • 1970-01-01
      • 2011-12-18
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多