Configuring per site templates with Sites framework

The “sites” framework documentation has an example that indicates that templates can be loaded based on the site in the request:

Note that an even more flexible (but more heavyweight) way of doing this would be to use Django’s template system. Assuming Lawrence.com and LJWorld.com have different template directories ( DIRS ), you could farm out to the template system…

It’s not clear on how to fully configure the templates DIRS setting an use the Site object in the example.

This is my standard TEMPLATES setting across projects:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [str(ROOT_DIR / "templates")],
        "OPTIONS": {
            "loaders": [
                "django.template.loaders.filesystem.Loader",
                "django.template.loaders.app_directories.Loader",
            ],
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.static",
                "django.template.context_processors.media",
            ],
        },
    },
]

Following the docs example, in a mutli-site configuration, would the TEMPLATE setting be like this:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            str(ROOT_DIR / "templates/siteD.com"),
            str(ROOT_DIR / "templates/siteJ.com"),
        ],
        "OPTIONS": {
            "loaders": [
                "django.template.loaders.filesystem.Loader",
                "django.template.loaders.app_directories.Loader",
            ],
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.static",
                "django.template.context_processors.media",
            ],
        },
    },
]

I’ve used sites on projects but never to load site specific templates based on the request. I might be overlooking something obvious or maybe there is some additional documentation that can be addd to clarify the configuration in the example (I can contribute once I fully understand how this functionality works).

I imagine it assumes that you’re configuring the current site based on the SITE_ID setting rather than using shortcuts.get_current_site which requires the request. Then however you’re changing the SITE_ID per settings file/configuration, you’d also change the DIRS setting in that same manner.

As far as I’m aware, the template system doesn’t allow per request definitions of the directories to look for templates.

I think you’re correct. In the first example of the Getting the current domain for display it uses
from django.contrib.sites.shortcuts import get_current_site to provide context. The 2nd example within the section does not build upon example 1 but suggests a completely different approach and settings configuration.

I’ve implemented site specific template assignment in views and I was rereading Sites docs and I thought maybe I had overlooked a method to make this easier. As the docs note: It’s a good idea to exploit the Site objects as much as possible, to remove unneeded complexity and redundancy.