Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Django 1.2 and Jinja2 loader
Using Jinja2 with Django 1.2
Based on:
To use:
* Add this template loader to settings: `TEMPLATE_LOADERS`
* Add template dirs to settings: `JINJA2_TEMPLATE_DIRS`
If in template debug mode - we fire the template rendered signal, which allows
debugging the context with the debug toolbar. Viewing source currently doesnt
If you want {% url %} or {% csrf_token %} support I recommend grabbing them
from Coffin (
Note for namespaced urls you have to use quotes eg:
{% url account:login %} => {% url "account:login" %}
import jinja2
from django.template.loader import BaseLoader
from django.template import TemplateDoesNotExist, Origin
from django.core import urlresolvers
from django.conf import settings
class Template(jinja2.Template):
def render(self, context):
# flatten the Django Context into a single dictionary.
context_dict = {}
for d in context.dicts:
if settings.TEMPLATE_DEBUG:
from django.test import signals
self.origin = Origin(self.filename)
signals.template_rendered.send(sender=self, template=self, context=context)
return super(Template, self).render(context_dict)
class Loader(BaseLoader):
A file system loader for Jinja2.
Requires the following setting `JINJA2_TEMPLATE_DIRS`
is_usable = True
# Set up the jinja env and load any extensions you may have
env = jinja2.Environment(
env.template_class = Template
# These are available to all templates.
env.globals['url_for'] = urlresolvers.reverse
env.globals['MEDIA_URL'] = settings.MEDIA_URL
def load_template(self, template_name, template_dirs=None):
template = self.env.get_template(template_name)
return template, template.filename
except jinja2.TemplateNotFound:
raise TemplateDoesNotExist(template_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.