Login

Jinja2 Django integration

Author:
mathwizard
Posted:
September 17, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

Integration of django and Jinja2.

Just import render_to_response from this file and you are ready.

This file automatically adds template search path from yout TEMPLATE_DIRS and from each installed application.

You can also use several variables in your settings.py to add filters, tests and global functions to the default context. This can be done by using JINJA_GLOBALS, JINJA_FILTERS and JINJA_TEST e.g. JINJA_FILTERS = ( 'myapp.filters.myfilter', 'myapp.filters.myfilter2', )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from django.http import HttpResponse
from django.conf import settings
from jinja2 import PackageLoader, Environment, ChoiceLoader, FileSystemLoader
import os
from django.core.urlresolvers import get_callable
from djangotags import *
from django.utils import translation
from django.utils.thread_support import currentThread

global env

# Setup template loaders

loader_array = []
for pth in getattr(settings, 'TEMPLATE_DIRS', ()):
    loader_array.append(FileSystemLoader(pth))

for app in settings.INSTALLED_APPS:
    loader_array.append(PackageLoader(app))

# Setup environment

default_mimetype = getattr(settings, 'DEFAULT_CONTENT_TYPE')

global_exts = getattr(settings, 'JINJA_EXTS', ())

env = Environment(extensions=global_exts, loader=ChoiceLoader(loader_array))

if 'jinja2.ext.i18n' in global_exts:
    env.install_gettext_translations(translation)

# Add user Globals, Filters, Tests
global_imports = getattr(settings, 'JINJA_GLOBALS', ())
for imp in global_imports:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.globals[method_name] = method
    else:
        env.globals[method.__name__] = method

global_filters = getattr(settings, 'JINJA_FILTERS', ())
for imp in global_filters:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.filters[method_name] = method
    else:
        env.filters[method.__name__] = method

global_tests = getattr(settings, 'JINJA_TESTS', ())
for imp in global_tests:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.tests[method_name] = method
    else:
        env.tests[method.__name__] = method


def render_to_string(filename, context={}):
    template = env.get_template(filename)
    rendered = template.render(**context)
    return rendered

def render_to_response(filename, context={},mimetype=default_mimetype, request = None):
    if request:
        context['request'] = request
    rendered = render_to_string(filename, context)
    return HttpResponse(rendered,mimetype=mimetype)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

mathwizard (on September 18, 2008):

Hello, thanks, I haven't been aware of that script, but I personally like mine better, as it automatically includes each applications template dir, and lets you specify custom filters, tests, etc in your project settings.

#

Please login first before commenting.