Выберите базовый шаблон для каждого сайта

Я создаю установку, которая будет содержать основной сайт и несколько микросайтов. Каждый микросайт будет иметь отдельный брендинг, но использовать те же типы страниц.

Учитывая, что трясогузка уже имеет объект Site, который ссылается на соответствующее дерево Page, есть ли также встроенные функции для настройки загрузчиков шаблонов для выбора подходящего base.html или мне придется написать собственный загрузчик шаблонов?


person Danielle Madeley    schedule 15.02.2016    source источник


Ответы (2)



Я расширил приведенный выше ответ, чтобы обеспечить переопределение тега {% extends ... %} для использования параметра template_dir.

myapp.models:

from wagtail.contrib.settings.models import BaseSetting, register_setting

@register_setting
class SiteSettings(BaseSetting):
    """Site settings for each microsite."""

    # Database fields
    template_dir = models.CharField(max_length=255,
                                    help_text="Directory for base template.")

    # Configuration
    panels = ()

myapp.templatetags.local:

from django import template
from django.core.exceptions import ImproperlyConfigured
from django.template.loader_tags import ExtendsNode
from django.template.exceptions import TemplateSyntaxError


register = template.Library()


class SiteExtendsNode(ExtendsNode):
    """
    An extends node that takes a site.
    """

    def find_template(self, template_name, context):

        try:
            template_dir = \
                context['settings']['cms']['SiteSettings'].template_dir
        except KeyError:
            raise ImproperlyConfigured(
                "'settings' not in template context. "
                "Did you forget the context_processor?"
            )

        return super().find_template('%s/%s' % (template_dir, template_name),
                                     context)


@register.tag
def siteextends(parser, token):
    """
    Inherit a parent template using the appropriate site.
    """

    bits = token.split_contents()

    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' takes one argument" % bits[0])

    parent_name = parser.compile_filter(bits[1])
    nodelist = parser.parse()

    if nodelist.get_nodes_by_type(ExtendsNode):
        raise TemplateSyntaxError(
            "'%s' cannot appear more than once in the same template" % bits[0])

    return SiteExtendsNode(nodelist, parent_name)

мой проект. настройки:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            ...
            'builtins': ['myapp.templatetags.local'],
        },
    },
]
person Danielle Madeley    schedule 15.02.2016