Opened 4 years ago

Closed 4 years ago

#32131 closed Bug (wontfix)

ManifestStaticFilesStorage should also check in debug mode whether referenced file actually exists

Reported by: Fabian Köster Owned by: nobody
Component: contrib.staticfiles Version: 3.1
Severity: Normal Keywords:
Cc: Markus Bertheau Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We just had a case where we referenced a static file somewhere in the code like this

from django.templatetags.static import static
static("/path/to/a/file.png")

This worked perfectly fine on the development system and also on the staging systems. After deployment to the production system we noticed that it does not work when debug mode is disabled (debug = False) and the staticfiles are searched using the staticfiles.json manifest. In the code above we used an absolute path to the file whereas in the manifest all files are given with their relative path.

The following code can be used to reproduce the problem (using force=True is equivalent to debug=False in this case):

from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
>>> ManifestStaticFilesStorage().url("path/to/a/file.png")
'/static/path/to/a/file.png'
>>> ManifestStaticFilesStorage().url("/path/to/a/file.png")
'/static/path/to/a/file.png'
>>> ManifestStaticFilesStorage().url("path/to/a/file.png", force=True)
'/static/path/to/a/file.f208556b8835.png'
>>> ManifestStaticFilesStorage().url("/path/to/a/file.png", force=True)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    ManifestStaticFilesStorage().url('/path/to/a/file.png', force=True)
  File "/app/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/contrib/staticfiles/storage.py", line 153, in url
    return self._url(self.stored_name, name, force)
  File "/app/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/contrib/staticfiles/storage.py", line 132, in _url
    hashed_name = hashed_name_func(*args)
  File "/app/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/contrib/staticfiles/storage.py", line 420, in stored_name
    raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for '/path/to/a/file.png'

I think the behaviour should be the same regardless if debug is True or False. I therefore propose to add a check to the debug = True code path that checks for the existance of the file as well so when referencing a non-existing file or giving an absolute path raises the same exception as it would on the production system with debug = False. I will create a PR for this in a few minutes.

Change History (4)

comment:1 by Fabian Köster, 4 years ago

Version: 3.12.2

comment:2 by Fabian Köster, 4 years ago

Version: 2.23.1

comment:3 by Markus Bertheau, 4 years ago

Cc: Markus Bertheau added

comment:4 by Carlton Gibson, 4 years ago

Resolution: wontfix
Status: newclosed

Hi Fabian. I think we have to save wontfix here - this is expected behaviour.

Passing an absolute path to static isn’t correct: all paths should be relative to STATIC_ROOT (which is quite insistent that it ends in a `/`, so that relative paths can be appended). All the doc examples show using a relative path. Your examples using relative paths all work as expected so...

I think any change here would need to go via the DevelopersMailingList. Thanks.

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