Search code examples
djangodjango-custom-tags

Unable to call custom template tag


In a html page, I'm trying to call django custom template tag but it seems to me that its never reaching that template tag function.

home.html page

{% 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

from django import template

from users.models import Profile

register = template.Library()

@register.simple_tag
def has_profile():
    return 1

Please let me know if you need any information. Thanks!


Solution

  • That's not how it works. Anything in an if tag must be a template variable, not a tag.

    You can save the result of your tag to a variable using the as syntax and use that:

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