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',
)
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.