Where do I put jinja2 helpers in a django 1.8 project?
NickName:edruid Ask DateTime:2015-10-07T20:45:33

Where do I put jinja2 helpers in a django 1.8 project?

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

More about “Where do I put jinja2 helpers in a django 1.8 project?” related questions

Where do I put jinja2 helpers in a django 1.8 project?

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 su...

Show Detail

How to use jinja2 as a templating engine in Django 1.8

I have been looking on how to use jinja2 in django 1.8, but there is no complete source for using django with jinja2. I was wondering if you guys knew the process for using jinja2 in django. I have

Show Detail

Django 1.8 with Jinja2: Contrib app Admin not working

I upgraded to a fresh install of Django 1.8 and began using Jinja2 as it said that it was supported now and Jinja2 has some features I could use in my project. After finishing adapting the templat...

Show Detail

Django 1.8 not using Jinja2 template engine

I have followed the instructions on using jinja2 with django1.8. ---> #settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [

Show Detail

Using django-paging extension with Django and Jinja2/Coffin

Recently I switched my templating engine from default to Jinja2/Coffin. Everything works just fine but I'm having troubles trying to use Django/Jinja2 django-paging (http://linux.softpedia.com/get/

Show Detail

Setup Jinja2 in Django Project

Newbie question, yet I cannot find a sufficient step-by-step instruction on Jinja2 official website or by googling. My current Django project is halfway done, and I cannot stand not able to use

Show Detail

Crispy forms with Jinja2 in Django 1.8

Now that Django has built-in support for Jinja2, does any one know of a way to use crispy forms with the Jinja2 template backend? I am really interested in using Jinja2, but would really like to u...

Show Detail

Jinja2 throwing templatedoesnotexist error in Django project

I'm trying to use jinja2 in my Django project (ver 1.10). After setting it up, once I try to run the project, I get TemplateDoesNotExist at /base/index.html and Template-loader postmortem Django...

Show Detail

Upgrading a django 1.4 project to 1.8 version

I am new to django and i have a django 1.4 version project. i am trying to run it in django 1.8 environment. I'd like to upgrade my 1.4 project to 1.8 without rewriting the project. If someone know...

Show Detail

Django project - 1.4 to 1.8?

I'm taking over somebody else's project that was coded using Django 1.4 and Python 2.6. Pretty old, i know. I want to upgrade it to Django 1.8 and Python 2.7. After installing Python 2.7 and Djang...

Show Detail