Skip to content

Commit cf01e19

Browse files
committed
Load templates from template engines’ DIRS setting
1 parent 984aa52 commit cf01e19

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- We now officially support Django 3.2, and tentatively Django 4.0
8+
- Load templates from template engines’ [`DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#dirs) as well as apps’ `templates` subdirectories.
89

910
### Fixed
1011

docs/reference/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ PATTERN_LIBRARY = {
5959
### `SECTIONS`
6060

6161
`SECTIONS` controls the groups of templates that appear in the navigation.
62-
The keys are the group titles and the values are lists of template name prefixes that will be searched to populate the groups.
62+
The keys are the group titles and the values are lists of template name prefixes that will be searched to populate the groups. The pattern library searches for templates both in [`DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#dirs) directories for template engines, and in the `templates` subdirectory inside each installed application if using [`APP_DIRS`](https://docs.djangoproject.com/en/3.2/ref/settings/#app-dirs).
6363

6464
You can use this to create basic two-folder "includes and pages" hierarchies:
6565

pattern_library/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import re
44

5+
from django.conf import settings
56
from django.template import TemplateDoesNotExist
67
from django.template.context import Context
78
from django.template.loader import get_template, render_to_string
@@ -66,9 +67,16 @@ def order_dict(dictionary, key_sort=None):
6667
return dict(values)
6768

6869

70+
def get_template_dirs():
71+
template_dirs = [d for engines in settings.TEMPLATES for d in engines.get("DIRS", [])]
72+
template_app_dirs = get_app_template_dirs('templates')
73+
template_dirs += template_app_dirs
74+
return template_dirs
75+
76+
6977
def get_pattern_templates():
7078
templates = base_dict()
71-
template_dirs = get_app_template_dirs('templates')
79+
template_dirs = get_template_dirs()
7280

7381
for lookup_dir in template_dirs:
7482
for root, dirs, files in os.walk(lookup_dir, topdown=True):

tests/tests/test_utils.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
from django.test import SimpleTestCase
1+
import os
22

3-
from pattern_library.utils import get_template_ancestors
3+
from django.conf import settings
4+
from django.test import SimpleTestCase, override_settings
5+
6+
from pattern_library.utils import get_template_ancestors, get_template_dirs
47

58

69
class TestGetTemplateAncestors(SimpleTestCase):
@@ -33,3 +36,40 @@ def test_parent_template_from_variable(self):
3336
'patterns/base.html',
3437
],
3538
)
39+
40+
@override_settings(TEMPLATES=[
41+
{
42+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
43+
'APP_DIRS': True,
44+
},
45+
])
46+
def test_get_template_dirs_app_dirs(self):
47+
template_dirs = ['/'.join(d.replace(os.path.dirname(settings.BASE_DIR), 'dpl').split('/')[-4:-1]) for d in get_template_dirs()]
48+
self.assertListEqual(template_dirs, [
49+
'django/contrib/auth',
50+
'dpl/pattern_library',
51+
'dpl/tests',
52+
])
53+
54+
@override_settings(TEMPLATES=[
55+
{
56+
'NAME': 'one',
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [os.path.join(settings.BASE_DIR, "test_one", "templates")],
59+
'APP_DIRS': True,
60+
},
61+
{
62+
'NAME': 'two',
63+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
64+
'DIRS': [os.path.join(settings.BASE_DIR, "test_two", "templates")],
65+
},
66+
])
67+
def test_get_template_dirs_list_dirs(self):
68+
template_dirs = ['/'.join(d.replace(os.path.dirname(settings.BASE_DIR), 'dpl').split('/')[-4:-1]) for d in get_template_dirs()]
69+
self.assertListEqual(template_dirs, [
70+
'dpl/tests/test_one',
71+
'dpl/tests/test_two',
72+
'django/contrib/auth',
73+
'dpl/pattern_library',
74+
'dpl/tests',
75+
])

0 commit comments

Comments
 (0)