Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #583 -- Added app_directories template loader, which searches f…

…or templates in 'templates' directory in each INSTALLED_APPS package. It's turned off by default.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@892 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 57b4f231fdc24dcb41a503c3314442ff5250a830 1 parent b9736c5
Adrian Holovaty authored October 17, 2005
1  django/conf/global_settings.py
@@ -65,6 +65,7 @@
65 65
 # See the comments in django/core/template/loader.py for interface
66 66
 # documentation.
67 67
 TEMPLATE_LOADERS = (
  68
+#     'django.core.template.loaders.app_directories.load_template_source',
68 69
     'django.core.template.loaders.filesystem.load_template_source',
69 70
 #     'django.core.template.loaders.eggs.load_template_source',
70 71
 )
7  django/conf/project_template/settings/main.py
@@ -30,6 +30,13 @@
30 30
 # Make this unique, and don't share it with anybody.
31 31
 SECRET_KEY = ''
32 32
 
  33
+# List of callables that know how to import templates from various sources.
  34
+TEMPLATE_LOADERS = (
  35
+#     'django.core.template.loaders.app_directories.load_template_source',
  36
+    'django.core.template.loaders.filesystem.load_template_source',
  37
+#     'django.core.template.loaders.eggs.load_template_source',
  38
+)
  39
+
33 40
 MIDDLEWARE_CLASSES = (
34 41
     "django.middleware.common.CommonMiddleware",
35 42
     "django.middleware.doc.XViewMiddleware",
28  django/core/template/loaders/app_directories.py
... ...
@@ -0,0 +1,28 @@
  1
+# Wrapper for loading templates from "template" directories in installed app packages.
  2
+
  3
+from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
  4
+from django.core.template import TemplateDoesNotExist
  5
+import os
  6
+
  7
+# At compile time, cache the directories to search.
  8
+app_template_dirs = []
  9
+for app in INSTALLED_APPS:
  10
+    i = app.rfind('.')
  11
+    m, a = app[:i], app[i+1:]
  12
+    mod = getattr(__import__(m, '', '', [a]), a)
  13
+    template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
  14
+    if os.path.isdir(template_dir):
  15
+        app_template_dirs.append(template_dir)
  16
+
  17
+# It won't change, so convert it to a tuple to save memory.
  18
+app_template_dirs = tuple(app_template_dirs)
  19
+
  20
+def load_template_source(template_name, template_dirs=None):
  21
+    for template_dir in app_template_dirs:
  22
+        filepath = os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
  23
+        try:
  24
+            return open(filepath).read()
  25
+        except IOError:
  26
+            pass
  27
+    raise TemplateDoesNotExist, template_name
  28
+load_template_source.is_usable = True

0 notes on commit 57b4f23

Please sign in to comment.
Something went wrong with that request. Please try again.