loader_array for pth in getattr settings TEMPLATE_DIRS loader_array ap

 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
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))
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)
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={}, request=None, mimetype=default_mimetype):
if request:
context['request'] = request
context['user'] = request.user
rendered = render_to_string(filename, context)
return HttpResponse(rendered,mimetype=mimetype)