summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-12-31 13:18:57 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-12-31 13:19:34 +0100
commit5fdc81d8930b85849a39e550fa54be1cad10d74d (patch)
treea9f7be3727fe88b724aa5ee21955c559f8975f95
parent6b4b7da740cea6d1fc7ae7028a357587f3e9d0b3 (diff)
[3.1.x] Fixed #32304 -- Fixed prefixing STATIC_URL and MEDIA_URL by SCRIPT_NAME for absolute URLs with no domain.
Thanks Adam Hooper for the report. Regression in c574bec0929cd2527268c96a492d25223a9fd576. Backport of e13b71403bd1568abed237858127677144d43d23 from master
-rw-r--r--django/conf/__init__.py13
-rw-r--r--docs/releases/3.1.5.txt6
-rw-r--r--tests/settings_tests/tests.py4
3 files changed, 12 insertions, 11 deletions
diff --git a/django/conf/__init__.py b/django/conf/__init__.py
index a10c8e5958..297d1e9bfd 100644
--- a/django/conf/__init__.py
+++ b/django/conf/__init__.py
@@ -15,8 +15,7 @@ from pathlib import Path
import django
from django.conf import global_settings
-from django.core.exceptions import ImproperlyConfigured, ValidationError
-from django.core.validators import URLValidator
+from django.core.exceptions import ImproperlyConfigured
from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.functional import LazyObject, empty
@@ -124,14 +123,8 @@ class LazySettings(LazyObject):
Useful when the app is being served at a subpath and manually prefixing
subpath to STATIC_URL and MEDIA_URL in settings is inconvenient.
"""
- # Don't apply prefix to valid URLs.
- try:
- URLValidator()(value)
- return value
- except (ValidationError, AttributeError):
- pass
- # Don't apply prefix to absolute paths.
- if value.startswith('/'):
+ # Don't apply prefix to absolute paths and URLs.
+ if value.startswith(('http://', 'https://', '/')):
return value
from django.urls import get_script_prefix
return '%s%s' % (get_script_prefix(), value)
diff --git a/docs/releases/3.1.5.txt b/docs/releases/3.1.5.txt
index eea060d112..82dc1f0254 100644
--- a/docs/releases/3.1.5.txt
+++ b/docs/releases/3.1.5.txt
@@ -16,3 +16,9 @@ Bugfixes
* Fixed a bug in Django 3.1 that caused a crash when processing middlewares in
an async context with a middleware that raises a ``MiddlewareNotUsed``
exception (:ticket:`32299`).
+
+* Fixed a regression in Django 3.1 that caused the incorrect prefixing of
+ ``STATIC_URL`` and ``MEDIA_URL`` settings, by the server-provided value of
+ ``SCRIPT_NAME`` (or ``/`` if not set), when set to a URL specifying the
+ protocol but without a top-level domain, e.g. ``http://myhost/``
+ (:ticket:`32304`).
diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py
index 55ca0de524..b397202e9e 100644
--- a/tests/settings_tests/tests.py
+++ b/tests/settings_tests/tests.py
@@ -577,10 +577,12 @@ class MediaURLStaticURLPrefixTest(SimpleTestCase):
set_script_prefix(val)
def test_not_prefixed(self):
- # Don't add SCRIPT_NAME prefix to valid URLs, absolute paths or None.
+ # Don't add SCRIPT_NAME prefix to absolute paths, URLs, or None.
tests = (
'/path/',
'http://myhost.com/path/',
+ 'http://myhost/path/',
+ 'https://myhost/path/',
None,
)
for setting in ('MEDIA_URL', 'STATIC_URL'):