-
-
Save tourist/2137459 to your computer and use it in GitHub Desktop.
MultiSitedMiddleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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