-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
_get_sidecar_url should probably use django.templatetags.static.static instead of STATIC_URL #718
Comments
excellent point @glennmatthews! thanks for pointing it out. |
Hmm... For me this PR actually breaks the correct behavior 😀 The code now references a subdirectory, but directories are missing in
|
Unix begs to differ 😆 Not quite sure on how to go from here. I don't really use this myself so I would need to take a closer look. @glennmatthews any comments? @paulsmirnov what do you think would be the correct handling? I mean that change seemed more in line with what Django is expecting. |
@tfranzel for my project I will try to create a (temporary?) workaround. I planned to inherit from ManifestStaticFilesStorage and add directories to the manifest (if it is possible) or/and add the files as exemptions. For your library... Well, I think Django expects you to wrap each file with the |
I hope I'll have more info when I finish my workaround. |
@paulsmirnov let me know your findings. I will set aside some time too. |
I went with a workaround for now. Just in case anybody stumbles upon this discussion, here is the code: from django.contrib.staticfiles.storage import ManifestFilesMixin, StaticFilesStorage
class MyFilesMixin(ManifestFilesMixin):
passthrough_names = [
'drf_spectacular_sidecar/swagger-ui-dist',
'drf_spectacular_sidecar/redoc',
]
def stored_name(self, name):
if name in self.passthrough_names:
return name
return super().stored_name(name)
class MyStaticFilesStorage(MyFilesMixin, StaticFilesStorage):
pass and then in settings.py use this class instead of -STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
+STATICFILES_STORAGE = 'path.to.storage.MyStaticFilesStorage' As for the possible fix in the library, @tfranzel I think you should consider changing the way you refer to static files in the templates. See https://docs.djangoproject.com/en/4.1/howto/static-files/ and https://stackoverflow.com/questions/16655851/django-1-5-how-to-use-variables-inside-static-tag for the details. E.g., for -<link rel="stylesheet" href="{{ dist }}/swagger-ui.css">
+<link rel="stylesheet" href="{% with dist|add:'/swagger-ui.css' as swagger_css %}{% static swagger_css %}{% endwith %}"> This will also require changing I could try this and prepare a PR in a while, though I'm afraid it will require thorough testing. |
Describe the bug
This may fail when using
STATICFILES_STORAGE
with a non-default backend for whichSTATIC_URL
is unused or is insufficient to describe the static file location. A more robust implementation would possibly be to usedjango.templatetags.static
, i.e.:To Reproduce
Install
django-storages
and configureSTATICFILES_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, usestaticfiles_storage.url()
instead ofSTATIC_URL
to construct the static file paths for sidecar files (just asdjango.templatetags.static.static()
does).The text was updated successfully, but these errors were encountered: