Skip to content

_get_sidecar_url should probably use django.templatetags.static.static instead of STATIC_URL #718

@glennmatthews

Description

@glennmatthews

Describe the bug

def _get_sidecar_url(package):
    return f'{settings.STATIC_URL}drf_spectacular_sidecar/{package}'

This may fail when using STATICFILES_STORAGE with a non-default backend for which STATIC_URL is unused or is insufficient to describe the static file location. A more robust implementation would possibly be to use django.templatetags.static, i.e.:

from django.templatetags.static import static

def _get_sidecar_url(package):
    return static(f'drf_spectacular_sidecar/{package}')

To Reproduce
Install django-storages and configure STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" (etc.) in your project settings. Do not set STATIC_URL in your project settings. Verify that other static files are correctly loaded from S3, but SpectacularSwaggerView is attempting incorrectly to load the sidecar files from the relative path of /static/ instead of from the S3 server.

Expected behavior
When STATICFILES_STORAGE is configured, use staticfiles_storage.url() instead of STATIC_URL to construct the static file paths for sidecar files (just as django.templatetags.static.static() does).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions