I need to add a very simple filter in jinja2. Basically, it takes a number and adds "+" if it is positive. I followed jinja2 docs on how to add custom filters, but it doesn't seem to work (on GAE).
Python:
def str_votes(votes):
if votes > 0:
return '+' + str(votes)
else:
return str(votes)
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape=True)
jinja_env.globals['str_votes'] = str_votes
HTML (for display page):
<div>{{ 123|str_votes }}</div>
This gives me an error: TemplateAssertionError: no filter named 'str_votes'
How to fix it? (There was a similar question here that was never answered.)
source
share