From 6fde6863c5684a3f8583d07efa79178129e128c1 Mon Sep 17 00:00:00 2001 From: Danielle Madeley Date: Tue, 15 Mar 2016 17:33:53 +1100 Subject: [PATCH] Get the request host from request.get_host Fixes #2358 --- wagtail/wagtailcore/models.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 67b33aaa9d..00e4299eea 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -108,13 +108,18 @@ class Site(models.Model): still be routed to a different hostname which is set as the default """ try: - hostname = request.META['HTTP_HOST'].split(':')[0] # KeyError here goes to the final except clause + hostname = request.get_host().split(':')[0] try: # find a Site matching this specific hostname return Site.objects.get(hostname=hostname) # Site.DoesNotExist here goes to the final except clause except Site.MultipleObjectsReturned: # as there were more than one, try matching by port too - port = request.META['SERVER_PORT'] # KeyError here goes to the final except clause + try: + port = request.get_port() + except AttributeError: + # Request.get_port is Django 1.9+ + # KeyError here falls out below + port = request.META['SERVER_PORT'] return Site.objects.get(hostname=hostname, port=int(port)) # Site.DoesNotExist here goes to the final except clause except (Site.DoesNotExist, KeyError):