0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Get the request host from request.get_host

Fixes #2358
This commit is contained in:
Danielle Madeley 2016-03-15 17:33:53 +11:00 committed by Alex Gleason
parent 5158ee7aad
commit 6fde6863c5

View File

@ -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):