Skip to content

Commit 9494ff3

Browse files
sarahboyceadamchainzArchmonger
authored
Use settings.FORCE_SCRIPT_NAME correctly (#486)
--------- Co-authored-by: Adam Johnson <[email protected]> Co-authored-by: Mark Bakhit <[email protected]>
1 parent c42e93c commit 9494ff3

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ Unreleased
1515

1616
Thanks to Per Myren for the detailed investigation and fix in `PR #612 <https://github.com/evansd/whitenoise/pull/612>`__.
1717

18+
* Use Django’s |FORCE_SCRIPT_NAME|__ setting correctly.
19+
This reverts a change from version 5.3.0 that added a call to Django’s |get_script_prefix() method|__ outside of the request-response cycle.
20+
21+
.. |FORCE_SCRIPT_NAME| replace:: ``FORCE_SCRIPT_NAME``
22+
__ https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME
23+
24+
.. |get_script_prefix() method| replace:: ``get_script_prefix()`` method
25+
__ https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix
26+
27+
Thanks to Sarah Boyce in `PR #486 <https://github.com/evansd/whitenoise/pull/486>`__.
28+
1829
6.7.0 (2024-06-19)
1930
------------------
2031

docs/django.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,10 @@ arguments upper-cased with a 'WHITENOISE\_' prefix.
460460
then ``WHITENOISE_STATIC_PREFIX`` will be ``/static/``.
461461

462462
If your application is not running at the root of the domain and
463-
``FORCE_SCRIPT_NAME`` is set then this value will be removed from the
464-
``STATIC_URL`` path first to give the correct prefix.
463+
``FORCE_SCRIPT_NAME`` is set, then this value will be removed from the
464+
``STATIC_URL`` path first, to give the correct prefix. For example, if
465+
``STATIC_URL`` is ``'apple/static/`` and ``FORCE_SCRIPT_NAME`` is
466+
``'/apple'``, then ``WHITENOISE_STATIC_PREFIX`` will be ``/static/``.
465467

466468
If your deployment is more complicated than this (for instance, if you are
467469
using a CDN which is doing path rewriting) then you may need to configure

src/whitenoise/middleware.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from django.contrib.staticfiles import finders
99
from django.contrib.staticfiles.storage import staticfiles_storage
1010
from django.http import FileResponse
11-
from django.urls import get_script_prefix
1211

1312
from whitenoise.base import WhiteNoise
1413
from whitenoise.string_utils import ensure_leading_trailing_slash
@@ -94,10 +93,10 @@ def __init__(self, get_response=None, settings=settings):
9493
self.static_prefix = settings.WHITENOISE_STATIC_PREFIX
9594
except AttributeError:
9695
self.static_prefix = urlparse(settings.STATIC_URL or "").path
97-
script_prefix = get_script_prefix().rstrip("/")
98-
if script_prefix:
99-
if self.static_prefix.startswith(script_prefix):
100-
self.static_prefix = self.static_prefix[len(script_prefix) :]
96+
if settings.FORCE_SCRIPT_NAME:
97+
script_name = settings.FORCE_SCRIPT_NAME.rstrip("/")
98+
if self.static_prefix.startswith(script_name):
99+
self.static_prefix = self.static_prefix[len(script_name) :]
101100
self.static_prefix = ensure_leading_trailing_slash(self.static_prefix)
102101

103102
self.static_root = settings.STATIC_ROOT

tests/test_django_whitenoise.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,11 @@ def test_relative_static_url(server, static_files, _collect_static):
209209
url = storage.staticfiles_storage.url(static_files.js_path)
210210
response = server.get(url)
211211
assert response.content == static_files.js_content
212+
213+
214+
@override_settings(FORCE_SCRIPT_NAME="/subdir", STATIC_URL="static/")
215+
def test_force_script_name(server, static_files, _collect_static):
216+
url = storage.staticfiles_storage.url(static_files.js_path)
217+
assert url.startswith("/subdir/static/")
218+
response = server.get(url)
219+
assert response.content == static_files.js_content

0 commit comments

Comments
 (0)