首页 > 解决方案 > 配置多站点模板加载

问题描述

目前的templates布局:

第二个站点将很快在different domain. 两个站点的逻辑是相同的。模板的名称是相同的。需要从first domain进入时,从 - 其模板进入时加载second domain其模板。如果没有这样的模板,加载一些通用模板。不需要复制模板。

apps
    app1
        templates
            folder_with_templates_1
                index.html
            admin
                index.html
    app2
        templates
            folder_with_templates_2
                index.html
            admin
                index.html
    templates
        admin
            index.html
        404.html
        500.html

什么是无痛的解决方案?

  1. 写点拐杖template loader?没有成功。如有必要,我可以列出代码。

  2. 拐杖Middleware,它将确定网站并加载适当的模板?

  3. dirs in the settings每个站点的配置?

  4. 找到 Django-multisite,但无法配置它。如有必要,我将显示我的设置。https://github.com/ecometrica/django-multisite

第一个站点的设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '..', 'apps', 'templates/')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # Project context processors
                'apps.landing.context_processors.landing_settings',
                'apps.landing.context_processors.page404_context',
                'apps.landing.context_processors.user_token_data',
            ],
        },
    },
]

标签: djangodjango-templatesmultisite

解决方案


模板的层次结构

apps
  app1
    templates
      site1
        app1
          index.html
      site2
        app1
          index.html
  app2
    templates
      site1
        app2
          index.html
      site2
        app2
          index.html
  ...
  templates
    index.html
    ...

设置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '..', 'apps', 'templates')],
        # 'APP_DIRS': True,
        'OPTIONS': {
            'loaders': ['apps.loaders.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',
                # Project context processors
                'apps.landing.context_processors.landing_settings',
                'apps.landing.context_processors.page404_context',
                'apps.landing.context_processors.user_token_data',
            ],
        },
    },
]

装载机

class Loader(FilesystemLoader):
    def get_path_dirs(self, template_path):
        return get_app_template_dirs(template_path)

    def get_template_sources(self, *args, **kwargs):
        start = args[0]

        template_path = 'templates/site_{}/'.format(Site.objects.get_current().id)

        for i in args[0].split('/'):
            if '.' in i:
                x = i
            else:
                template_path += i + '/'
        if self.get_path_dirs(template_path):
            template_name = self.get_path_dirs(template_path)[0] + x
        else:
            template_name = ''
            if Site.objects.get_current().id != 1:
                template_path = 'templates/site_1/'.format(Site.objects.get_current().id)

                for i in args[0].split('/'):
                    if '.' in i:
                        x = i
                    else:
                        template_path += i + '/'
                if self.get_path_dirs(template_path):
                    template_name = self.get_path_dirs(template_path)[0] + x
                else:
                    apps_folder = os.path.dirname(os.path.abspath(__file__)) + '/templates/' + x
                    if os.path.exists(apps_folder):
                        apps_folder = os.path.dirname(os.path.abspath(__file__)) + '/templates/' + x
                        template_name = os.path.exists(apps_folder)

        if django_version < (2, 0, 0):
            args = [template_name, None]
        else:
            args = [template_name]
        if args[0] != '':
            yield Origin(
                name=args[0],
                template_name=start,
                loader=self,
            )

推荐阅读