66from typing import Any
77
88from cachetools import LRUCache
9+ from django .apps import apps
910from django .conf import settings
1011from django .template .backends .django import Template as DjangoTemplate
12+ from django .template .engine import Engine
1113from django .template .exceptions import TemplateDoesNotExist
1214from django .template .loader import select_template
1315
@@ -51,8 +53,26 @@ class ComponentRegistry:
5153 def __init__ (self , maxsize : int = 100 ):
5254 self ._components : LRUCache [str , Component ] = LRUCache (maxsize = maxsize )
5355
56+ def get_dirs (self ):
57+ engine = Engine .get_default ()
58+ dirs : list [str | Path ] = list (engine .dirs )
59+
60+ for app_config in apps .get_app_configs ():
61+ template_dir = Path (app_config .path ) / "templates"
62+ if template_dir .is_dir ():
63+ dirs .append (template_dir )
64+
65+ base_dir = getattr (settings , "BASE_DIR" , None )
66+
67+ if base_dir is not None :
68+ root_template_dir = Path (base_dir ) / "templates"
69+ if root_template_dir .is_dir ():
70+ dirs .append (root_template_dir )
71+
72+ return dirs
73+
5474 def discover_components (self ) -> None :
55- template_dirs = [ dir for config in settings . TEMPLATES for dir in config [ "DIRS" ]]
75+ template_dirs = self . get_dirs ()
5676
5777 component_dirs = [
5878 Path (template_dir ) / component_dir
@@ -61,17 +81,15 @@ def discover_components(self) -> None:
6181 ]
6282
6383 for component_dir in component_dirs :
84+ component_dir = Path (component_dir )
85+
6486 if not component_dir .is_dir ():
6587 continue
6688
67- for component_path in component_dir .iterdir ():
68- if component_path .is_dir ():
69- component_name = component_path .name
70- elif component_path .suffix == ".html" :
71- component_name = component_path .stem
72- else :
73- continue
74-
89+ for component_path in component_dir .rglob ("*.html" ):
90+ component_name = str (
91+ component_path .relative_to (component_dir ).with_suffix ("" )
92+ )
7593 try :
7694 self .get_component (component_name )
7795 except TemplateDoesNotExist :
0 commit comments