Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16787 -- Restored the ability to {% load %} template tags libr…

…aries within packages. Thanks Ivan Sagalaev for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17133 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c5a899b190d1afa798efade499e8d00a5286fa28 1 parent 19e5408
Aymeric Augustin authored November 21, 2011
20  django/template/base.py
@@ -1215,6 +1215,22 @@ def render(self, context):
1215 1215
             return func
1216 1216
         return dec
1217 1217
 
  1218
+def is_library_missing(name):
  1219
+    """Check if library that failed to load cannot be found under any
  1220
+    templatetags directory or does exist but fails to import.
  1221
+
  1222
+    Non-existing condition is checked recursively for each subpackage in cases
  1223
+    like <appdir>/templatetags/subpackage/package/module.py.
  1224
+    """
  1225
+    # Don't bother to check if '.' is in name since any name will be prefixed
  1226
+    # with some template root.
  1227
+    path, module = name.rsplit('.', 1)
  1228
+    try:
  1229
+        package = import_module(path)
  1230
+        return not module_has_submodule(package, module)
  1231
+    except ImportError:
  1232
+        return is_library_missing(path)
  1233
+
1218 1234
 def import_library(taglib_module):
1219 1235
     """
1220 1236
     Load a template tag library module.
@@ -1222,8 +1238,6 @@ def import_library(taglib_module):
1222 1238
     Verifies that the library contains a 'register' attribute, and
1223 1239
     returns that attribute as the representation of the library
1224 1240
     """
1225  
-    app_path, taglib = taglib_module.rsplit('.', 1)
1226  
-    app_module = import_module(app_path)
1227 1241
     try:
1228 1242
         mod = import_module(taglib_module)
1229 1243
     except ImportError, e:
@@ -1231,7 +1245,7 @@ def import_library(taglib_module):
1231 1245
         # that's not an error that should be raised. If the submodule exists
1232 1246
         # and raised an ImportError on the attempt to load it, that we want
1233 1247
         # to raise.
1234  
-        if not module_has_submodule(app_module, taglib):
  1248
+        if is_library_missing(taglib_module):
1235 1249
             return None
1236 1250
         else:
1237 1251
             raise InvalidTemplateLibrary("ImportError raised loading %s: %s" %
5  docs/ref/templates/builtins.txt
@@ -698,9 +698,10 @@ load
698 698
 Loads a custom template tag set.
699 699
 
700 700
 For example, the following template would load all the tags and filters
701  
-registered in ``somelibrary`` and ``otherlibrary``::
  701
+registered in ``somelibrary`` and ``otherlibrary`` located in package
  702
+``package``::
702 703
 
703  
-    {% load somelibrary otherlibrary %}
  704
+    {% load somelibrary package.otherlibrary %}
704 705
 
705 706
 .. versionchanged:: 1.3
706 707
 
0  tests/regressiontests/templates/templatetags/subpackage/__init__.py
No changes.
7  tests/regressiontests/templates/templatetags/subpackage/echo.py
... ...
@@ -0,0 +1,7 @@
  1
+from django import template
  2
+
  3
+register = template.Library()
  4
+
  5
+@register.simple_tag
  6
+def echo2(arg):
  7
+    return arg
1  tests/regressiontests/templates/templatetags/subpackage/echo_invalid.py
... ...
@@ -0,0 +1 @@
  1
+import nonexistent.module
19  tests/regressiontests/templates/tests.py
@@ -1193,17 +1193,22 @@ def get_template_tests(self):
1193 1193
             'inheritance41': ("{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}", {'numbers': '123'}, '_new1_new2_new3_'),
1194 1194
 
1195 1195
             ### LOADING TAG LIBRARIES #################################################
  1196
+            'load01': ("{% load testtags subpackage.echo %}{% echo test %} {% echo2 \"test\" %}", {}, "test test"),
  1197
+            'load02': ("{% load subpackage.echo %}{% echo2 \"test\" %}", {}, "test"),
1196 1198
 
1197 1199
             # {% load %} tag, importing individual tags
1198  
-            'load1': ("{% load echo from testtags %}{% echo this that theother %}", {}, 'this that theother'),
1199  
-            'load2': ("{% load echo other_echo from testtags %}{% echo this that theother %} {% other_echo and another thing %}", {}, 'this that theother and another thing'),
1200  
-            'load3': ("{% load echo upper from testtags %}{% echo this that theother %} {{ statement|upper }}", {'statement': 'not shouting'}, 'this that theother NOT SHOUTING'),
  1200
+            'load03': ("{% load echo from testtags %}{% echo this that theother %}", {}, 'this that theother'),
  1201
+            'load04': ("{% load echo other_echo from testtags %}{% echo this that theother %} {% other_echo and another thing %}", {}, 'this that theother and another thing'),
  1202
+            'load05': ("{% load echo upper from testtags %}{% echo this that theother %} {{ statement|upper }}", {'statement': 'not shouting'}, 'this that theother NOT SHOUTING'),
  1203
+            'load06': ("{% load echo2 from subpackage.echo %}{% echo2 \"test\" %}", {}, "test"),
1201 1204
 
1202 1205
             # {% load %} tag errors
1203  
-            'load4': ("{% load echo other_echo bad_tag from testtags %}", {}, template.TemplateSyntaxError),
1204  
-            'load5': ("{% load echo other_echo bad_tag from %}", {}, template.TemplateSyntaxError),
1205  
-            'load6': ("{% load from testtags %}", {}, template.TemplateSyntaxError),
1206  
-            'load7': ("{% load echo from bad_library %}", {}, template.TemplateSyntaxError),
  1206
+            'load07': ("{% load echo other_echo bad_tag from testtags %}", {}, template.TemplateSyntaxError),
  1207
+            'load08': ("{% load echo other_echo bad_tag from %}", {}, template.TemplateSyntaxError),
  1208
+            'load09': ("{% load from testtags %}", {}, template.TemplateSyntaxError),
  1209
+            'load10': ("{% load echo from bad_library %}", {}, template.TemplateSyntaxError),
  1210
+            'load11': ("{% load subpackage.echo_invalid %}", {}, template.TemplateSyntaxError),
  1211
+            'load12': ("{% load subpackage.missing %}", {}, template.TemplateSyntaxError),
1207 1212
 
1208 1213
             ### I18N ##################################################################
1209 1214
 

0 notes on commit c5a899b

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