Skip to main content

Load templates from site labels if available

Project description

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 # extends _base.html, overrides nothing +—- _base.html

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-label-templates-1.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

django_label_templates-1.0.2-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-label-templates-1.0.2.tar.gz.

File metadata

File hashes

Hashes for django-label-templates-1.0.2.tar.gz
Algorithm Hash digest
SHA256 3fcdcc9ac851e50b956fb0f7cc7b8e108733661a4612bf45af67cfad10e804e7
MD5 9aa54c5fdc55067793c7c50d3aa69717
BLAKE2b-256 5d30fce4f621c5dfbd89642e88b638a2de55923a10db43b436057c122108db1e

See more details on using hashes here.

File details

Details for the file django_label_templates-1.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_label_templates-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 244e271ca6affbc3f097f028c4263abde462c351a513d2b037ee99876de45c00
MD5 0ee76704a5b8fc656185923616a7f746
BLAKE2b-256 16318a7ec0757dfbffd615b131c8aa06d063e01c904d4cf30cf7739e3bfd3ae8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page