浏览代码

Refs #32987 -- Refactored out get_template_tag_modules().

Daniel Fairhead 3 年之前
父节点
当前提交
b98394fa62
共有 1 个文件被更改,包括 19 次插入10 次删除
  1. 19 10
      django/template/backends/django.py

+ 19 - 10
django/template/backends/django.py

@@ -84,18 +84,16 @@ def reraise(exc, backend):
     raise new from exc
 
 
-def get_installed_libraries():
+def get_template_tag_modules():
     """
-    Return the built-in template tag libraries and those from installed
-    applications. Libraries are stored in a dictionary where keys are the
-    individual module names, not the full module paths. Example:
-    django.templatetags.i18n is stored as i18n.
+    Yield (module_name, module_path) pairs for all installed template tag
+    libraries.
     """
-    libraries = {}
     candidates = ['django.templatetags']
     candidates.extend(
-        '%s.templatetags' % app_config.name
-        for app_config in apps.get_app_configs())
+        f'{app_config.name}.templatetags'
+        for app_config in apps.get_app_configs()
+    )
 
     for candidate in candidates:
         try:
@@ -106,9 +104,20 @@ def get_installed_libraries():
 
         if hasattr(pkg, '__path__'):
             for name in get_package_libraries(pkg):
-                libraries[name[len(candidate) + 1:]] = name
+                yield name[len(candidate) + 1:], name
+
 
-    return libraries
+def get_installed_libraries():
+    """
+    Return the built-in template tag libraries and those from installed
+    applications. Libraries are stored in a dictionary where keys are the
+    individual module names, not the full module paths. Example:
+    django.templatetags.i18n is stored as i18n.
+    """
+    return {
+        module_name: full_name
+        for module_name, full_name in get_template_tag_modules()
+    }
 
 
 def get_package_libraries(pkg):