evansd / whitenoise

Radically simplified static file serving for Python web apps
https://whitenoise.readthedocs.io
MIT License
2.51k stars 148 forks source link

Warning about Django's STATIC_ROOT directory not found when WHITENOISE_USE_FINDERS is true #215

Open dmsimard opened 5 years ago

dmsimard commented 5 years ago

Thanks for developing whitenoise :+1:

I have a use case where I'd like whitenoise to transparently handle all the static files for a Django application. The amount of static files is minimal and gets little to no traffic.

Reproducing this particular issue should be fairly easy:

Running manage.py runserver will raise a warning like this:

.tox/runserver/lib/python3.6/site-packages/whitenoise/base.py:104: UserWarning: No directory at: /var/www/static
  warnings.warn(u'No directory at: {}'.format(root))

The warning isn't a blocker: it doesn't prevent runserver or a wsgi server from starting. The warning might not be relevant when WHITENOISE_USE_FINDERS is True, though.

dmsimard commented 5 years ago

Link to relevant bit of code: https://github.com/evansd/whitenoise/blob/0e94fc16a6c47b78ec1fe818d087ce79914ff8f4/whitenoise/base.py#L101-L105

evansd commented 5 years ago

Unfortunately there's no completely clean way of dealing with this. Ideally we'd set STATIC_ROOT to None as a way of signalling that we never intend to write anything to it and then WhiteNoise would ignore the setting. But Django throws an ImproperlyConfigured exception if you try to do that. So you have to set it to a filesystem path, and WhiteNoise then has no way of knowing that you intended the directory to be missing.

Probably the easiest thing to do is to silence the warning by adding something like the following to you settings file:

import warnings
warnings.filterwarnings("ignore", message="No directory at", module="whitenoise.base" )
dmsimard commented 5 years ago

@evansd that's not a bad idea. For the time being we had worked around it by making sure that STATIC_ROOT was created with something like:

if not os.path.isdir(STATIC_ROOT):
    os.makedirs(STATIC_ROOT, mode=0o755)

Thanks for considering the issue -- if there's no obvious fix maybe we could document the workaround ?

pawelad commented 4 years ago

My pytest solution (just put it in your conftest.py):

import pytest

@pytest.fixture(autouse=True)
def whitenoise_autorefresh(settings):
    """
    Get rid of whitenoise "No directory at" warning, as it's not helpful when running tests.

    Related:
        - https://github.com/evansd/whitenoise/issues/215
        - https://github.com/evansd/whitenoise/issues/191
        - https://github.com/evansd/whitenoise/commit/4204494d44213f7a51229de8bc224cf6d84c01eb
    """
    settings.WHITENOISE_AUTOREFRESH = True
Aditya-Rajgor commented 3 years ago

When you want to practice on ur local server ,Set Debug=True, and run server again. Problem Solved And When you deploy into Heroku or any other pltfrm. Set Debug = False. This will Secure Ur Webpage ;)

mrbazzan commented 3 years ago

This works for me

from django.test.utils import ignore_warnings
ignore_warnings(message="No directory at", module="whitenoise.base").enable()
zamirszn commented 1 year ago

im currently having this same issue while deploying to railway.app

diegobarbo commented 1 year ago

im currently having this same issue while deploying to railway.app

me too... oh God !!!

Ian2012 commented 1 year ago

@ZamirSZN @diegobarbo We are three now...

zamirszn commented 1 year ago

@Ian2012 @diegobarbo did you guys get to fix it , i did by using the heroku build pack

sangkips commented 1 year ago

I am getting the same issue while running unit test

achingachris commented 7 months ago

Unfortunately there's no completely clean way of dealing with this. Ideally we'd set STATIC_ROOT to None as a way of signalling that we never intend to write anything to it and then WhiteNoise would ignore the setting. But Django throws an ImproperlyConfigured exception if you try to do that. So you have to set it to a filesystem path, and WhiteNoise then has no way of knowing that you intended the directory to be missing.

Probably the easiest thing to do is to silence the warning by adding something like the following to you settings file:

import warnings
warnings.filterwarnings("ignore", message="No directory at", module="whitenoise.base" )

Okay ... Getting this error on railway now ...