Fix Site.find_for_request to not assume that request.META will include an HTTP_HOST - the django test client doesn't, by default

pull/85/merge
Matt Westcott 2014-02-21 11:40:11 +00:00
rodzic e980b0d9da
commit 9aaa8e13f9
1 zmienionych plików z 5 dodań i 3 usunięć

Wyświetl plik

@ -45,12 +45,14 @@ class Site(models.Model):
@staticmethod
def find_for_request(request):
"""Find the site object responsible for responding to this HTTP request object"""
hostname = request.META['HTTP_HOST'].split(':')[0]
try:
hostname = request.META['HTTP_HOST'].split(':')[0]
# find a Site matching this specific hostname
return Site.objects.get(hostname=hostname)
except Site.DoesNotExist:
# failing that, look for a catch-all Site. If that fails, let the Site.DoesNotExist propagate back to the caller
except (Site.DoesNotExist, KeyError):
# If no matching site exists, or request does not specify an HTTP_HOST (which
# will often be the case for the Django test client), look for a catch-all Site.
# If that fails, let the Site.DoesNotExist propagate back to the caller
return Site.objects.get(is_default_site=True)
@property