-
Notifications
You must be signed in to change notification settings - Fork 53
Description
Issue Summary
For Jinja2 support, when using APP_DIRS=True, patterns are not found.
I have a project that is deployed as an installed app package, there is no DIRS templates directory, only APP_DIRS directories inside the apps. For the Jinja2 engine, this directory is 'jinja2', not 'templates'.
Steps to Reproduce
- New Django project eg as per Django Tutorial
- Set up Jinja as template engine as per normal instructions
- Install django-pattern-library and set it up as per docs.
- Add a "jinja2" directory inside the project's app, and add the patterns for the library inside that directory.
- When the project runs and you try to load the pattern library, it returns the empty pattern library error.
Expected behaviour is that it should discover the patterns inside the app's "jinja2" template directory like it would for a "templates" directory in the Django template engine.
Technical details
- Python version: 3.14.3
- Django version: 6.0.2
- Jinja2 version: 3.1.6
What's happening here is django-pattern-library is using its own discovery function in utils.py:get_template_dirs(), which hard-codes the app directory as "templates". But in Jinja2, it's "jinja2".
In my project, I monkey-patched this to a very simple brute-force fix by calling:
def get_template_dirs() -> tuple[str, ...]:
return (*original_get_template_dirs(), *get_app_template_dirs("jinja2"))
The fix could be that simple, but I tend to think this ignores the root cause - it would be better for django-pattern-library to interrogate the actual engines in use for the correct paths, rather than re-implementing the discovery in a utils function.
The django.template.backends.base:BaseEngine class defines a template_dirs property that returns "a list of directories to search for templates". I think this would be a better solution - and probably a way to also override the discovery eg an option in PATTERN_LIBRARY to point to a callable that returns the template directories to look in (making it easy to get backwards compatible behaviour by setting that to "pattern_library.utils.get_template_dirs").