Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#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 :

  1. create a django app with static directory containing image or css files.
  2. 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/)

  1. serve using gunicorn by previously setting SCRIPT_NAME environment variable to /django
  2. access the url served by gunicorn (http://localhost:8000/django/<appname>)
  3. the application urls are served under /django prefix, the generated static urls also having the /django/static prefix.
  4. accessing application url, gunicorn correctly renders pages, but not the static content

suggestion :

  1. 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
  2. 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)

urls.py (770 bytes ) - added by yudhiwidyatama 9 months ago.
this is my change suggestion in contrib/staticfiles/urls.py

Download all attachments as: .zip

Change History (3)

by yudhiwidyatama, 9 months ago

Attachment: urls.py added

this is my change suggestion in contrib/staticfiles/urls.py

comment:1 by Mariusz Felisiak, 9 months ago

Cc: Florian Apolloner Markus Holtermann added
Resolution: invalid
Status: newclosed

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 yudhiwidyatama, 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 ?

Note: See TracTickets for help on using tickets.
Back to Top