Unknown tag 'with'

When trying to use jinja2 templates with django 1.8 "Unknown unknown tag" with error the following error appears.

The same template works fine on my flash app, but when trying to use with jinja 2 functionality, I get this error.

inside jinja template

{% with %}
    {% set vartest = 42 %}
    {{ vartest }}
{% endwith %}

      

inside my jinja2 environment setup

def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url_for': reverse,
        'STATIC_URL': STATIC_URL
    })
    return env

      

+3


source to share


2 answers


The operator was new in version 2.3 of Jinja; if you have something before use to get the latest version. with

pip install --upgrade Jinja2

It's also an extension , so you'll need to include it in Environment

eg. adding:



options.setdefault('extensions', []).append('jinja2.ext.with_')

      

+5


source


This can also be configured in the settings file.



TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            'your/django/templates',
        ],
        'APP_DIRS': True,
    },
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [
            'your/jinja2/templates.',
        ],
        'OPTIONS':{
            'environment': 'app.project.jinja2.environment',
            'extensions': ['jinja2.ext.with_']}
    }
]

      

+1


source







All Articles