Skip to content

Commit 5fdc81d

Browse files
committed
[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 c574bec. Backport of e13b714 from master
1 parent 6b4b7da commit 5fdc81d

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

django/conf/__init__.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
import django
1717
from django.conf import global_settings
18-
from django.core.exceptions import ImproperlyConfigured, ValidationError
19-
from django.core.validators import URLValidator
18+
from django.core.exceptions import ImproperlyConfigured
2019
from django.utils.deprecation import RemovedInDjango40Warning
2120
from django.utils.functional import LazyObject, empty
2221

@@ -124,14 +123,8 @@ def _add_script_prefix(value):
124123
Useful when the app is being served at a subpath and manually prefixing
125124
subpath to STATIC_URL and MEDIA_URL in settings is inconvenient.
126125
"""
127-
# Don't apply prefix to valid URLs.
128-
try:
129-
URLValidator()(value)
130-
return value
131-
except (ValidationError, AttributeError):
132-
pass
133-
# Don't apply prefix to absolute paths.
134-
if value.startswith('/'):
126+
# Don't apply prefix to absolute paths and URLs.
127+
if value.startswith(('http://', 'https://', '/')):
135128
return value
136129
from django.urls import get_script_prefix
137130
return '%s%s' % (get_script_prefix(), value)

docs/releases/3.1.5.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ Bugfixes
1616
* Fixed a bug in Django 3.1 that caused a crash when processing middlewares in
1717
an async context with a middleware that raises a ``MiddlewareNotUsed``
1818
exception (:ticket:`32299`).
19+
20+
* Fixed a regression in Django 3.1 that caused the incorrect prefixing of
21+
``STATIC_URL`` and ``MEDIA_URL`` settings, by the server-provided value of
22+
``SCRIPT_NAME`` (or ``/`` if not set), when set to a URL specifying the
23+
protocol but without a top-level domain, e.g. ``http://myhost/``
24+
(:ticket:`32304`).

tests/settings_tests/tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,12 @@ def set_script_name(self, val):
577577
set_script_prefix(val)
578578

579579
def test_not_prefixed(self):
580-
# Don't add SCRIPT_NAME prefix to valid URLs, absolute paths or None.
580+
# Don't add SCRIPT_NAME prefix to absolute paths, URLs, or None.
581581
tests = (
582582
'/path/',
583583
'http://myhost.com/path/',
584+
'http://myhost/path/',
585+
'https://myhost/path/',
584586
None,
585587
)
586588
for setting in ('MEDIA_URL', 'STATIC_URL'):

0 commit comments

Comments
 (0)