Django custom template tag

Alex

I have defined a custom template tag, in a file called custom_tags.py:

from django.conf import settings
from django import template

register = template.Library()

@register.simple_tag
def currencysymbol():
    if settings.LANGUAGE_CODE == 'en-gb':
        return '£'
    else:
        return 'unknown'

Which is referenced in a template:

{% load custom_tags %}
{{ currencysymbol }}

The problem, is that it doesn't render. It is certainly loading the library, as I modified the load to tag to {% load foo %} and it correctly told me that this library does not exist, and listed 'custom_tags' as one of the available options.

However, {{ currencysymbol }} renders to nothing at all. To ensure that it wasn't my function, I modified it to simply return a string (without the if/else and the settings. stuff), but it still rendered nothing.

I believe that I have followed the docs, so I'm not sure what's happening. The page renders without errors, but my tag is simply not there.

Willem Van Onsem

You defined a tag, so that means you should use it like:

{% currencysymbol %}

So with the {% .. %} brackets, not:

{{ currencysymbol }}

The {{ .. }} is used for variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related