Skip to main content

A custom template loader for multi-site set-ups.

https://travis-ci.org/maykinmedia/django-label-loaders.svg?branch=master https://codecov.io/github/maykinmedia/django-label-loaders/coverage.svg?branch=master https://coveralls.io/repos/maykinmedia/django-label-loaders/badge.svg?branch=master&service=github https://img.shields.io/pypi/v/django-label-templates.svg

This is intended for users of django.contrib.sites for multi-site cases where a different site can have different templates, not only a different styling.

The custom template loaders will try to find a template for the currently active site, and fall back on a generic template.

Installation

Install with pip:

pip install django-label-templates

Configuration

Installed apps

Make sure that django.contrib.sites is in your INSTALLED_APPS.

Specify template loaders

You also need to enable the loader in the settings, similar to Django’s cached template loader. The loader takes an iterable (list or tuple) of loaders to find the templates. Example - and probably most used - set-up:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # APP_DIRS must be False if you specify the loaders yourself
        'APP_DIRS': False,
        'DIRS': [
            os.path.join(PROJECT_DIR, 'templates'),
        ],
        'OPTIONS': {
            'loaders': [
                ('label_templates.loaders.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],
        },
    },
]

If you wish, this template loader can also be wrapped in other loaders, like django.template.loaders.cached.Loader.

Configure the site labels

For the final step, one extra custom settings is required: SITELABELS.

# the import is safe and does NOT depend on django.conf.settings
from label_templates.sites import SiteLabels, SiteChoice

class SITELABELS(SiteLabels):
    site = SiteChoice('example.com', site_id=1)
    site2 = SiteChoice('example2.com', site_id=2)

This settings is used to look up the label-prefix for the templates. The site with ID=1 will resolve to a subfolder site/, so to load the template example.html, the loader will try to find site/example.html first, and failing that, will try to just load example.html.

For the site with ID=2, the prefix site2 is used.

Limitations

Since the {% extends %} tag uses the same loader configuration, you can not let a label-specific template inherit from a generic template with the same name.

A workaround is the following structure:

templates/
├── mylabel/
|   └── base.html  # extends _base.html and overrides stuff
├── _base.html
└── base.html  # extends _base.html, overrides nothing

Versions supported

This library is tested against Django 1.8 and 1.9. The corresponding Python versions for the Django versions apply. Consult tox.ini for an up to date build matrix.

Release files for django-label-templates 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-label-templates 1.0.3
File Size Uploaded
django-label-templates-1.0.3.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-label-templates 1.0.3
File Interpreter ABI Platform
django_label_templates-1.0.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.2 kB

Release files / django-label-templates-1.0.3.tar.gz

Download URL django-label-templates-1.0.3.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b1a2ee1dab1eadbf9a8ac0051e99b35f5e57c0f8e9579b04481344b3d966006e
BLAKE2b-256 checksum
How to use checksums
f8a9a022375e4b2745c05bd2f869b09510bd806c9a580b5c3faa056eb726fddc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_label_templates-1.0.3-py2.py3-none-any.whl

Download URL django_label_templates-1.0.3-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
99d43d9296d477e20ccd4f631ffdcd4b3ad189bc8e6fe72a12320592a6207a2d
BLAKE2b-256 checksum
How to use checksums
74b93806a528687888f10a2097ba55bea7e1611dad1394200a94dad6cb38b9cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page