Saturday, February 28, 2015

[1.8] MultiHost with template dir per Host

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



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/68d650a9-db37-46b9-8407-9eafd1404158%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment