#35312 closed Bug (invalid)
staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using SCRIPT_NAME
Reported by: | yudhiwidyatama | Owned by: | nobody |
---|---|---|---|
Component: | contrib.staticfiles | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Florian Apolloner, Markus Holtermann | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Deploying Django using Gunicorn with SCRIPT_NAME set to '/django', we get the url for static resources are correctly generated with '/django/static/' prefix, but the static url patterns is also prefixed with the '/django/static/'. This is wrong because url patterns will be compared with the PATH_INFO part of the URL without the script_name prefix.
Reproduce step :
- create a django app with static directory containing image or css files.
- append static url pattern to site's urls.py by either appending
urlpatterns += staticfiles_urlpatterns()
or
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
(refer: https://docs.djangoproject.com/en/5.0/howto/static-files/)
- serve using gunicorn by previously setting SCRIPT_NAME environment variable to /django
- access the url served by gunicorn (http://localhost:8000/django/<appname>)
- the application urls are served under /django prefix, the generated static urls also having the /django/static prefix.
- accessing application url, gunicorn correctly renders pages, but not the static content
suggestion :
- change staticfiles_urlpatterns() in contrib/staticfiles/urls.py to check whether the urlpatterns prefix starts with get_script_prefix(), and if so, strip the get_script_prefix() part from prefix
- or change the settings.STATIC_URL in the howto page with expression that strips the SCRIPT_NAME part or get_script_prefix() part.
Attachments (1)
Change History (3)
by , 9 months ago
comment:1 by , 9 months ago
Cc: | added |
---|---|
Resolution: | → invalid |
Status: | new → closed |
Thanks for this ticket, however, the proposed change is backward incompatible and incorrect. SCRIPT_NAME
prefix should be added to STATIC_URL
and MEDIA_URL
set to relative paths and that's how it works now, see related #25598 and #32394. If you don't want to serve them on a SCRIPT_NAME
subpath, you should use absolute paths in these settings.
comment:2 by , 9 months ago
I am not proposing to remove SCRIPT_NAME from the STATIC_URL, but only to adjust the url pattern generated by staticfiles_urlpatterns. If I may ask, for what condition the staticfiles_urlpatterns needs SCRIPT_NAME prefix ?
this is my change suggestion in contrib/staticfiles/urls.py