【问题标题】:Unable to call custom template tag无法调用自定义模板标签
【发布时间】:2019-11-16 19:48:28
【问题描述】:

在 html 页面中,我试图调用 django 自定义模板标签,但在我看来,它从未达到该模板标签功能。

home.html 页面

{% load custom_tags %}
{% if has_profile %}
  <p> Creator </p>
{% else %}
  <li><a href="{% url 'update_profile' %}" class="btn btn-simple">Become a Creator</a></li>
{% endif %}

custom_tags.py

从 django 导入模板

from users.models import Profile

register = template.Library()

@register.simple_tag
def has_profile():
    return 1

如果您需要任何信息,请告诉我。谢谢!

【问题讨论】:

    标签: django django-custom-tags


    【解决方案1】:

    这不是它的工作原理。 if 标签中的任何内容都必须是模板变量,而不是标签。

    您可以使用as 语法将标签的结果保存到变量中并使用它:

    {% has_profile as has_profile_result %}
    {% if has_profile_result %}
        ...
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2020-09-25
      • 2013-05-30
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2014-01-12
      • 2018-06-07
      相关资源
      最近更新 更多