I'm currently upgrading django from 1.7.10 with jingo to 1.8.5 (actually from 1.6.x but I can now run the site with 1.7)
jingo no longer works with django 1.8 so I'm switching to the new Jinja2 support in django 1.8. I've reached a point where my templates are found and they are attempting to render using jinja2 but my helpers (contextfunction
) are not found.
My TEMPLATES
config looks like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
os.path.join(PROJECT_ROOT, 'mysite', 'html'),
os.path.join(PROJECT_ROOT, 'mysite', 'html', 'site')
],
'APP_DIRS': False,
'OPTIONS': {
'extensions': [
'pipeline.templatetags.ext.PipelineExtension',
'mysite.site.extensions.CacheTag',
'mysite.site.extensions.CmsBlockTag',
],
'extensions': [
'jinja2.ext.autoescape',
'jinja2.ext.do',
'jinja2.ext.with_'
],
},
}
]
I currently have a file mysite/site/helpers.py
that contains (amongst other functions and filters):
from jinja2 import contextfunction
@contextfunction
def upcase(text):
return text.upper()
And a template:
<h3>{{ upcase('asdf') }}</h3>
I get the error Exception Value: 'upcase' is undefined
.
How do I make the contents of the helpers file available?
Copyright Notice:Content Author:「edruid」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32992593/where-do-i-put-jinja2-helpers-in-a-django-1-8-project