Create a gist now

Instantly share code, notes, and snippets.

Jinja2 template loader for django
# First try to load with our loader then fallback to django default templates
# We should use another (not .html) extension for our Jinja2 templates in order to make this work
# these loaders here for admin, contrib and third-party apps
# These settings work as you expect for both Jinja2 and Django templates
HOME_DIR + '/templates'
# And these are for Jinja2 only
# use django filters in jinja
'floatformat': 'django.template.defaultfilters.floatformat',
'slugify': 'django.template.defaultfilters.slugify',
# add your custom ones here
# You can also just list them, function names will be used as filter names:
#JINJA2_FILTERS = ('django.template.defaultfilters.floatformat',
# 'django.template.defaultfilters.slugify')
# Used similar to filters
'now': '',
# and add some options here
'autoescape': True,
# an example is borrowed from django tutorial
from django.shortcuts import render_to_response, get_object_or_404
# Using Django templates
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/detail.html', {'poll': p})
# Using Jinja2 templates
# Any extension but ".html" will do. I simply like .j2
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/detail.j2', {'poll': p})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment