Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created January 25, 2011 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ciantic/795138 to your computer and use it in GitHub Desktop.
Save Ciantic/795138 to your computer and use it in GitHub Desktop.
MultiSitedMiddleware
"""Allows serving multiple sites per Django instance
"""
from django.utils.cache import patch_vary_headers
from sitecache import get_site_id, get_urlconf
class MultiSitedMiddleware:
def process_request(self, request):
"""Augments Request object with urlconf and site_id
"""
host = request.META.get("HTTP_HOST", "")
# Remove :80 port, other ports are left as is
if host.endswith(":80"):
host = host[:-3]
# Try to get special urlconf, site_id
request.urlconf = get_urlconf(host)
request.site_id = get_site_id(host)
def process_response(self, request, response):
if hasattr(request, "urlconf") or hasattr(request, "site_id"):
patch_vary_headers(response, ('Host',))
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment