Skip to content

Commit 8f22916

Browse files
committed
Avoid duplicating templates when more than one template engine is set up
1 parent 0182512 commit 8f22916

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Avoid duplicating templates when more than one template engine is set up
8+
9+
510
## [1.5.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.5.0) - 2025-04-08
611

712
### Added

pattern_library/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import operator
22
import os
33
import re
4+
from pathlib import Path
45

56
from django.conf import settings
67
from django.template import TemplateDoesNotExist
@@ -75,9 +76,11 @@ def get_template_dirs():
7576
template_dirs = [
7677
d for engines in settings.TEMPLATES for d in engines.get("DIRS", [])
7778
]
79+
template_dirs_paths = [Path(d).absolute() for d in template_dirs]
7880
template_app_dirs = get_app_template_dirs("templates")
79-
template_dirs += template_app_dirs
80-
return template_dirs
81+
# Use set to avoid duplicates in case more than one engine is used and
82+
# both find the same dirs
83+
return list(set(template_dirs_paths).union(set(template_app_dirs)))
8184

8285

8386
def get_pattern_config_str(template_name):

tests/tests/test_utils.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def get_relative_template_dirs(self):
6565
]
6666
)
6767
def test_get_template_dirs_app_dirs(self):
68-
self.assertListEqual(
69-
self.get_relative_template_dirs(),
70-
[
68+
self.assertEqual(
69+
set(self.get_relative_template_dirs()),
70+
{
7171
"django/contrib/auth",
7272
"dpl/pattern_library",
7373
"dpl/tests",
74-
],
74+
},
7575
)
7676

7777
@override_settings(
@@ -90,15 +90,40 @@ def test_get_template_dirs_app_dirs(self):
9090
]
9191
)
9292
def test_get_template_dirs_list_dirs(self):
93-
self.assertListEqual(
94-
self.get_relative_template_dirs(),
95-
[
93+
self.assertEqual(
94+
set(self.get_relative_template_dirs()),
95+
{
9696
"dpl/tests/test_one",
9797
"dpl/tests/test_two",
9898
"django/contrib/auth",
9999
"dpl/pattern_library",
100100
"dpl/tests",
101-
],
101+
},
102+
)
103+
104+
@override_settings(
105+
TEMPLATES=[
106+
{
107+
"BACKEND": "django.template.backends.django.DjangoTemplates",
108+
"APP_DIRS": True,
109+
"DIRS": [os.path.join(settings.BASE_DIR, "test_one", "templates")],
110+
},
111+
{
112+
"BACKEND": "django.template.backends.jinja2.Jinja2",
113+
"APP_DIRS": True,
114+
"DIRS": [os.path.join(settings.BASE_DIR, "test_one", "templates")],
115+
},
116+
]
117+
)
118+
def test_get_template_dirs_with_two_engines(self):
119+
self.assertEqual(
120+
set(self.get_relative_template_dirs()),
121+
{
122+
"dpl/tests/test_one",
123+
"django/contrib/auth",
124+
"dpl/pattern_library",
125+
"dpl/tests",
126+
},
102127
)
103128

104129

0 commit comments

Comments
 (0)