whitenoise
whitenoise copied to clipboard
Warning shown if STATIC_ROOT directory missing in development
Hi! :-)
STR:
- Update existing Django+WhiteNoise project from whitenoise 3.3.1 to 4.0.
- Don't run collectstatic (ie since developing locally), so the directory referenced by
STATIC_ROOT
doesn't yet exist - With Django's
DEBUG = True
, run./manage.py runserver
Expected:
No console warnings about a missing static files directory.
Actual:
Warning in the console:
...
Django version 1.11.15, using settings 'treeherder.config.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
/usr/local/lib/python2.7/site-packages/whitenoise/base.py:97: UserWarning: No directory at: /app/treeherder/static/
warnings.warn(u'No directory at: {}'.format(root))
Notes:
- The warning was added to
add_files()
in 2a883f5cda0336dbefebef7bb918c3a2b167a70d. - We don't pre-create the
STATIC_ROOT
directory, since:- Django's
collectstatic
handles the creation of the directory automatically - To create the directory we'd either need to use the
.gitkeep
type approach (which then causescollectstatic
to output misleading warnings/prompts about overwriting existing files) or else have additional local setup steps prior to running./manage.py runserver
.
- Django's
A possible solution might be to have .add_files()
take an optional ignore_missing=False
argument, which is left as False
everywhere apart from the Django middleware static root usage. eg:
if self.static_root:
self.add_files(self.static_root, prefix=self.static_prefix, ignore_missing=self.autorefresh)
That said, should static_root
even be added when run in DEBUG
mode? The Django docs say not to place anything in the STATIC_ROOT
directory directly, so it feels like there shouldn't be content there (though how many people go against that, I don't know).
Thanks Ed, I hadn't considered this situation. I wonder if the correct behaviour would be just not to warn about missing directories if autorefresh
is enabled. What do you think?
Yeah I think skipping the warning entirely if autorefresh
enabled, might be the simplest thing :-)
Hi
I've just started a new project with Whitenoise 4.0 and have exactly this problem - will there be a release that removes this warning in local dev?
Thanks.
@urlsangel Yep, the 4.1 release should be out soon which will include this fix
@evansd
Great stuff, thanks for your work on this! 👍
@evansd - I'm using version 5.2.0 and still seeing this warning, is there something that I need to set on my end to eliminate this?
I can confirm what @Br4nd0R is saying. This issue is still there in 5.2.0
Same here - 5.2.0 - I'm guessing a recent commit accidentally put it back in?
Still here in 6.2.
I noticed this while running tests locally.
This can be worked around either by:
- Running
./manage.py collectstatic
and having this dir exist before running tests. - Define a test runner which is referenced using the
TEST_RUNNER
setting, and in that runner add the setting:WHITENOISE_AUTOREFRESH = True
As for why this warning is being triggered even though it was though to have been fixed:
WhiteNoise.add_files()
was changed late 2018 (after 4.1) from defining the root
directory as:
root = root.rstrip(os.path.sep) + os.path.sep
to its current:
root = os.path.abspath(root)
The difference between these in 3.11
is the current version returns the path without the final forward slash.
That said, the test of existence, os.path.isdir(root)
returns False for me either way (triggering a warning in each case).
The original commit merged in 4.1 seems to have included a test stub.
Currently, the test that should cover tests for the existence of a single warning.
I wonder if a warning is being issued but it isn't the one expected by the failed directory check