Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure relative STATIC_URLs are supported #259

Merged
merged 1 commit into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/test_django_whitenoise.py
Expand Up @@ -209,3 +209,12 @@ def test_whitenoise_file_response_has_only_one_header():
# This subclass should have none of the default headers that FileReponse
# sets
assert headers == {"content-type"}


@pytest.mark.skipif(django.VERSION[:2] < (3, 1), reason="feature added in Django 3.1")
@override_settings()
def test_relative_static_url(server, static_files, _collect_static):
settings.STATIC_URL = "static/"
url = storage.staticfiles_storage.url(static_files.js_path)
response = server.get(url)
assert response.content == static_files.js_content
9 changes: 5 additions & 4 deletions whitenoise/middleware.py
Expand Up @@ -6,6 +6,7 @@
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles import finders
from django.http import FileResponse
from django.urls import get_script_prefix

from .base import WhiteNoise
from .string_utils import decode_if_byte_string, ensure_leading_trailing_slash
Expand Down Expand Up @@ -82,10 +83,10 @@ def configure_from_settings(self, settings):
self.autorefresh = settings.DEBUG
self.use_finders = settings.DEBUG
self.static_prefix = urlparse(settings.STATIC_URL or "").path
if settings.FORCE_SCRIPT_NAME:
script_name = settings.FORCE_SCRIPT_NAME.rstrip("/")
if self.static_prefix.startswith(script_name):
self.static_prefix = self.static_prefix[len(script_name) :]
script_prefix = get_script_prefix().rstrip("/")
if script_prefix:
if self.static_prefix.startswith(script_prefix):
self.static_prefix = self.static_prefix[len(script_prefix) :]
if settings.DEBUG:
self.max_age = 0
# Allow settings to override default attributes
Expand Down