Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Fixed

- Avoid duplicating templates when more than one template engine is set up


## [1.5.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.5.0) - 2025-04-08

### Added
Expand Down
7 changes: 5 additions & 2 deletions pattern_library/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import operator
import os
import re
from pathlib import Path

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


def get_pattern_config_str(template_name):
Expand Down
41 changes: 33 additions & 8 deletions tests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def get_relative_template_dirs(self):
]
)
def test_get_template_dirs_app_dirs(self):
self.assertListEqual(
self.get_relative_template_dirs(),
[
self.assertEqual(
set(self.get_relative_template_dirs()),
{
"django/contrib/auth",
"dpl/pattern_library",
"dpl/tests",
],
},
)

@override_settings(
Expand All @@ -90,15 +90,40 @@ def test_get_template_dirs_app_dirs(self):
]
)
def test_get_template_dirs_list_dirs(self):
self.assertListEqual(
self.get_relative_template_dirs(),
[
self.assertEqual(
set(self.get_relative_template_dirs()),
{
"dpl/tests/test_one",
"dpl/tests/test_two",
"django/contrib/auth",
"dpl/pattern_library",
"dpl/tests",
],
},
)

@override_settings(
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
"DIRS": [os.path.join(settings.BASE_DIR, "test_one", "templates")],
},
{
"BACKEND": "django.template.backends.jinja2.Jinja2",
"APP_DIRS": True,
"DIRS": [os.path.join(settings.BASE_DIR, "test_one", "templates")],
},
]
)
def test_get_template_dirs_with_two_engines(self):
self.assertEqual(
set(self.get_relative_template_dirs()),
{
"dpl/tests/test_one",
"django/contrib/auth",
"dpl/pattern_library",
"dpl/tests",
},
)


Expand Down
Loading