0

在 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

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

4

1 回答 1

0

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

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

{% has_profile as has_profile_result %}
{% if has_profile_result %}
    ...
于 2019-07-06T16:04:10.967 回答