[Django] #35312: staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using SCRIPT_NAME

6 views
Skip to first unread message

Django

unread,
Mar 16, 2024, 7:09:52 AM3/16/24
to django-...@googlegroups.com
#35312: staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using
SCRIPT_NAME
-----------------------------------------------+------------------------
Reporter: yudhiwidyatama | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
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/)
3. serve using gunicorn by previously setting SCRIPT_NAME environment
variable to /django
4. access the url served by gunicorn
(http://localhost:8000/django/<appname>)
5. the application urls are served under /django prefix, the generated
static urls also having the /django/static prefix.
6. accessing application url, gunicorn correctly renders pages, but not
the static content

suggestion :
a. 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
b. or change the settings.STATIC_URL in the howto page with expression
that strips the SCRIPT_NAME part or get_script_prefix() part.
--
Ticket URL: <https://code.djangoproject.com/ticket/35312>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 16, 2024, 7:11:09 AM3/16/24
to django-...@googlegroups.com
#35312: staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using
SCRIPT_NAME
-------------------------------------+-------------------------------------
Reporter: yudhiwidyatama | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by yudhiwidyatama):

* Attachment "urls.py" added.

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

Django

unread,
Mar 18, 2024, 12:48:40 AM3/18/24
to django-...@googlegroups.com
#35312: staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using
SCRIPT_NAME
-------------------------------------+-------------------------------------
Reporter: yudhiwidyatama | Owner: nobody
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Florian Apolloner, Markus Holtermann (added)
* resolution: => invalid
* status: new => closed

Comment:

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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35312#comment:1>

Django

unread,
Mar 18, 2024, 7:20:11 PM3/18/24
to django-...@googlegroups.com
#35312: staticurl_urlpatterns incorrectly uses STATIC_URL that prefixed using
SCRIPT_NAME
-------------------------------------+-------------------------------------
Reporter: yudhiwidyatama | Owner: nobody
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by yudhiwidyatama):

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 ?
--
Ticket URL: <https://code.djangoproject.com/ticket/35312#comment:2>
Reply all
Reply to author
Forward
0 new messages