Custom template directory name in apps

42 views
Skip to first unread message

Wouter Roos

unread,
May 9, 2022, 7:25:16 PMMay 9
to Django developers (Contributions to Django itself)
Feature:
Configure name of the templates directory in which the app directory template loader looks for matching template names. Possibly from the template settings.

Current situation:
Currently the directory name ('templates') is hardcoded in the app directory loader (template/loaders/app_directories.py) as a fixed value in a method, thus adjusting it requires a custom class that overrides the method. As django templates default to a configurable file loader (django.template.loaders.filesystem.Loader), I always thought it was odd that the same did not apply to the app directory template loader.

Why I think it's beneficial
Django can support multiple template backends. This can be useful when supporting multiple template situations. I myself like to differentiate my normal front-page templates from my e-mail templates. However, I've run into the issue on three different projects where I could not place a mail template folder in my application folders unless I wrote a custom loader.
I think allowing a loader to have the template folder name as input can allow cleaner code.

What I propose
Essence: Rewrite the app directory template loader to allow the template folder name as input as defined in the ticket created.
Example template settings:

"OPTIONS": {

"loaders": [
("django.template.loaders.app_directories.Loader", "mail_templates") ],
},

Optional: Rewrite DjangoTemplates to allow dir_name as input to define the directory name for template folders in the applications.

Sources


Let me know if you think this is a good idea. I'd love to hear  your opinions :D

Mariusz Felisiak

unread,
May 10, 2022, 1:27:06 AMMay 10
to Django developers (Contributions to Django itself)
Hi,

    Thanks for the proposition. I'm not sure why the new option is better than using subdirectories in "templates"  e.g. "templates/mail/".

Best,
Mariusz

Adam Johnson

unread,
May 10, 2022, 9:26:24 AMMay 10
to Django developers (Contributions to Django itself)
I agree with Mariusz.

Moreover, the app directories loader class is really small. You can create a project-specific version in 3 lines, if you need:

class Loader(FilesystemLoader):
    def get_dirs(self):
        return get_app_template_dirs("email-templates")

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/fb7408c2-aa2d-4dfb-a624-a042a68e5b55n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages