Skip to content

Template directory discovery does not find Jinja2 app directories #269

@LucidDan

Description

@LucidDan

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").

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions