Code


Version 3 (modified by foxwhisper, 3 years ago) (diff)

--

# File: multihost.py

##
# A simple middleware component that lets you use a single Django
# instance to server multiple distinct hosts.
# 6th July 2011: IMPORTANT!! Make sure this is the FIRST entry in your MIDDLEWARE_CLASSES
# - Cal
##

from django.conf import settings
from django.utils.cache import patch_vary_headers

class MultiHostMiddleware:

    def process_request(self, request):
        try:
            host = request.META["HTTP_HOST"]
            if host[-3:] == ":80":
                host = host[:-3] # ignore default port number, if present
            request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host]
        except KeyError:
            pass # use default urlconf (settings.ROOT_URLCONF)

    def process_response(self, request, response):
        if getattr(request, "urlconf", None):
            patch_vary_headers(response, ('Host',))
        return response