[1.8] MultiHost with template dir per Host

90 views
Skip to first unread message

Neyoui

unread,
Mar 1, 2015, 6:31:57 PM3/1/15
to django...@googlegroups.com
Hi,

I had create a middleware, that is able to change the template directory per host on the fly.
But it stopped working since I upgraed to Django 1.8 and I tried to fix it.

My problem is:
If I visit domain1.tld, all works fine. Django loads the templates from the right directory. (/path/domain1.tld/templates/)
But if I visit now domain2.tld, Django tried to load the template from the domain1.tld directory /path/domain1.tld/templates/ and not from /path/domain2.tld/templates/.
The debug messages shows me, that the middleware overrides the template path successfully.

Maybe someone can help me to fix it.

Details:
Django version: 1.8b1
Python: 3.4

If you need some other information, just ask for it.

- settings.py
MIDDLEWARE_CLASSES = (
'core.middleware.MultiHostMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'debug': True
        },
    },
]

ROOT_URLCONF = 'core.urls'

HOST_MIDDLEWARE_TEMPLATE_DIRS = {
   "domain1.tld": BASE_DIR + "/domain1.tld/templates/",
   "domain2.tld": BASE_DIR + "/domain2.tld/templates/",
}

HOST_MIDDLEWARE_URLCONF_MAP = {
   "domain1.tld": "domain1.urls",
   "domain2.tld": "domain2.urls",
}

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'core',
    'domain1',
    'domain2',
)

- middleware.py
from django.conf import settings
from django.utils.cache import patch_vary_headers


class MultiHostMiddleware:

    def get_settings(self, request):

        host = request.META["HTTP_HOST"]
        host_port = host.split(':')

        if len(host_port) == 2:
            host = host_port[0]

        if host in settings.HOST_MIDDLEWARE_URLCONF_MAP:

            urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host]
            template_dirs = settings.HOST_MIDDLEWARE_TEMPLATE_DIRS[host],

        else:
            urlconf = settings.ROOT_URLCONF
            template_dirs = None

        return urlconf, tuple(template_dirs)

    def process_request(self, request):

        urlconf, template_dirs = self.get_settings(request)

        request.urlconf = urlconf
        settings.TEMPLATES[0]['DIRS'] = template_dirs

    def process_response(self, request, response):

        if getattr(request, "urlconf", None):
            patch_vary_headers(response, ('Host',))

        return response



Collin Anderson

unread,
Mar 6, 2015, 5:16:00 PM3/6/15
to django...@googlegroups.com
Hi,

You might need to modify the template engine directly.

Collin
Reply all
Reply to author
Forward
0 new messages