diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2015-05-03 23:08:28 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-06-18 10:18:12 -0400 |
| commit | b3d5dc6932bf896a909e9871d508654494b34563 (patch) | |
| tree | d995b27a4f985faa8b3edcb98c38ba2cf799f817 /django | |
| parent | 7f1168e387dc1db70b6093cfd23a4a6978f48109 (diff) | |
Fixed #24834 -- Fixed get_current_site() when Host header contains port.
When the Host header contains a port, looking up the Site record fails
as the host will never match the domain.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/sites/models.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py index 6471dca136..e22fd4021d 100644 --- a/django/contrib/sites/models.py +++ b/django/contrib/sites/models.py @@ -5,6 +5,7 @@ import string from django.core.exceptions import ImproperlyConfigured, ValidationError from django.db import models from django.db.models.signals import pre_delete, pre_save +from django.http.request import split_domain_port from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -37,10 +38,19 @@ class SiteManager(models.Manager): def _get_site_by_request(self, request): host = request.get_host() - if host not in SITE_CACHE: - site = self.get(domain__iexact=host) - SITE_CACHE[host] = site - return SITE_CACHE[host] + try: + # First attempt to look up the site by host with or without port. + if host not in SITE_CACHE: + SITE_CACHE[host] = self.get(domain__iexact=host) + return SITE_CACHE[host] + except Site.DoesNotExist: + # Fallback to looking up site after stripping port from the host. + domain, port = split_domain_port(host) + if not port: + raise + if domain not in SITE_CACHE: + SITE_CACHE[domain] = self.get(domain__iexact=domain) + return SITE_CACHE[domain] def get_current(self, request=None): """ |
