torchbox / torchbox/django-pattern-library

Template directory discovery does not find Jinja2 app directories

Open
#269 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
423
Forks
53
PR merge metrics
No merged PRs in 30d

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
  1. New Django project eg as per Django Tutorial
  2. Set up Jinja as template engine as per normal instructions
  3. Install django-pattern-library and set it up as per docs.
  4. Add a "jinja2" directory inside the project's app, and add the patterns for the library inside that directory.
  5. 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").

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in utils.py at get_template_dirs(), then read Django's django.template.backends.base.BaseEngine.template_dirs and the existing get_app_template_dirs("templates") behavior. Determine how the active template engines should supply directories, including an installed app's jinja2 directory, and verify that the pattern library discovers patterns without requiring DIRS.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.