Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dmsimard opened this issue Mar 7, 2019 · 12 comments

Comments

@dmsimard
Copy link

dmsimard commented Mar 7, 2019

Thanks for developing whitenoise 馃憤

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:

  • whitenoise.middleware.WhiteNoiseMiddleware in the settings.py MIDDLEWARE list
  • WHITENOISE_USE_FINDERS = True in settings.py
  • Set STATIC_ROOT to a path that doesn't exist yet (because Django creates this only when running collectstatic, which I'd actually like to avoid)

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
Copy link
Author

dmsimard commented Mar 7, 2019

Link to relevant bit of code:

else:
if os.path.isdir(root):
self.update_files_dictionary(root, prefix)
else:
warnings.warn(u'No directory at: {}'.format(root))

@evansd
Copy link
Owner

evansd commented Mar 14, 2019

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
Copy link
Author

@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
Copy link

pawelad commented Nov 26, 2019

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
Copy link

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
Copy link

mrbazzan commented May 4, 2021

This works for me

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

@zamirszn
Copy link

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

@diegobarbo
Copy link

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

me too... oh God !!!

@Ian2012
Copy link

Ian2012 commented Oct 11, 2022

@zamirszn @diegobarbo We are three now...

@zamirszn
Copy link

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

@sangkips
Copy link

I am getting the same issue while running unit test

@achingachris
Copy link

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 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants