diff --git a/django/template/__init__.py b/django/template/__init__.py
index 29daa76..3e48bb5 100644
a
|
b
|
from django.utils.encoding import smart_unicode, force_unicode
|
59 | 59 | from django.utils.translation import ugettext as _ |
60 | 60 | from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping |
61 | 61 | from django.utils.html import escape |
| 62 | from django.templatetags import get_app_label_and_modules |
62 | 63 | |
63 | 64 | __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') |
64 | 65 | |
… |
… |
class Library(object):
|
910 | 911 | return func |
911 | 912 | return dec |
912 | 913 | |
913 | | def get_library(module_name): |
914 | | lib = libraries.get(module_name, None) |
| 914 | def import_library(module_name): |
| 915 | try: |
| 916 | mod = __import__(module_name, {}, {}, ['']) |
| 917 | except ImportError: |
| 918 | return None |
| 919 | try: |
| 920 | return mod.register |
| 921 | except AttributeError: |
| 922 | raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) |
| 923 | |
| 924 | def get_library(library_name): |
| 925 | lib = libraries.get(library_name, None) |
915 | 926 | if not lib: |
| 927 | |
| 928 | "Allow both library_name and app_label.library_name" |
916 | 929 | try: |
917 | | mod = __import__(module_name, {}, {}, ['']) |
918 | | except ImportError, e: |
919 | | raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) |
920 | | try: |
921 | | lib = mod.register |
922 | | libraries[module_name] = lib |
923 | | except AttributeError: |
924 | | raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) |
| 930 | app, library = library_name.split('.') |
| 931 | except: |
| 932 | app, library = ('', library_name) |
| 933 | |
| 934 | templatetags_modules = get_app_label_and_modules() |
| 935 | tried_modules = [] |
| 936 | for module, app_label in templatetags_modules: |
| 937 | if not app or app == app_label: |
| 938 | module_name = '%s.%s' % (module, library) |
| 939 | tried_modules.append(module_name) |
| 940 | lib = import_library(module_name) |
| 941 | if lib: |
| 942 | libraries[library_name] = lib |
| 943 | break |
| 944 | if not lib: |
| 945 | raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(tried_modules))) |
925 | 946 | return lib |
926 | 947 | |
927 | 948 | def add_to_builtins(module_name): |
928 | | builtins.append(get_library(module_name)) |
| 949 | builtins.append(import_library(module_name)) |
929 | 950 | |
930 | 951 | add_to_builtins('django.template.defaulttags') |
931 | 952 | add_to_builtins('django.template.defaultfilters') |
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index e5a8e66..cd671b9 100644
a
|
b
|
def load(parser, token):
|
850 | 850 | for taglib in bits[1:]: |
851 | 851 | # add the library to the parser |
852 | 852 | try: |
853 | | lib = get_library("django.templatetags.%s" % taglib) |
| 853 | lib = get_library(taglib) |
854 | 854 | parser.add_library(lib) |
855 | 855 | except InvalidTemplateLibrary, e: |
856 | 856 | raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % |
diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
index 9204535..321ed98 100644
a
|
b
|
|
1 | 1 | from django.conf import settings |
| 2 | import os |
2 | 3 | |
3 | | for a in settings.INSTALLED_APPS: |
4 | | try: |
5 | | __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) |
6 | | except ImportError: |
7 | | pass |
| 4 | templatetags_modules = [] |
| 5 | |
| 6 | def get_app_label_and_modules(): |
| 7 | if not templatetags_modules: |
| 8 | for a in ['django'] + list(settings.INSTALLED_APPS): |
| 9 | try: |
| 10 | module, app_label = (a, a.find('.')!=-1 and a.split('.')[-1] or a) |
| 11 | name = module + '.templatetags' |
| 12 | mod = __import__(name, {}, {}, ['']) |
| 13 | templatetags_modules.append((name, app_label)) |
| 14 | except ImportError: |
| 15 | pass |
| 16 | return templatetags_modules |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index aef6f50..0490f2a 100644
a
|
b
|
def do_echo(parser, token):
|
45 | 45 | |
46 | 46 | register.tag("echo", do_echo) |
47 | 47 | |
48 | | template.libraries['django.templatetags.testtags'] = register |
| 48 | template.libraries['testtags'] = register |
49 | 49 | |
50 | 50 | ##################################### |
51 | 51 | # Helper objects for template tests # |