Django has a belief that there is a SuspiciousFileOperation
error when this just is not the case. My fully reproducible setup is below. It continues to return this error despite the fact that:
- I have installed and set up
Whitenoise
correctly, as well as in theMiddleware
andInstalledApps
, in thesettings.py
filw, as well as the following:
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
MIDDLEWARE = [
"whitenoise.middleware.WhiteNoiseMiddleware",
]
STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_URL = 'static'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
- Despite the fact that I have correctly set up the static config in my root
urls.py
folder, as follows:
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
urlpatterns = [
......
]
# Only for development. Do not use this in production.
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My directory is as follows:
<PROJECTNAME>
<ROOTFOLDER>
settings.py
urls.py
<APP NAME>
static
file.css
static
libraries
css
image-video-hero.css
staticfiles
libraries
css
image-video-hero.css
Therefore, there is no reason as to why Django should have an issue with this, however, when I run the command of python manage.py collectstatic
command, I still get the trace back of:
File "<FILEPATH><PROJECTNAME>.venvLibsite-packagesdjangoutils_os.py", line 31, in safe_join
raise SuspiciousFileOperation(
django.core.exceptions.SuspiciousFileOperation: The joined path (<FILEPATH>staticlibrariescssimage-video-hero.css) is located outside of the base path component (<FILEPATH><PROJECTNAME>staticfiles)
Its makes absolutely no sense whatsoever.
In the STATiC_ROOT
variable, I have tried removing the forward slash, I have tried removing the trailing slash, I have tried removing both slashes at the same time. I have also tried this with the STATIC_URL
and STATICFILES_DIRS
settings.
I have tried rearranging the files in my static
folder, I have tried reading the Django documentation, I have tried the W3school tutorial, I have tried the 9,545,321,4546,999,3232,545,464 extremely inaccurate tutorials on YouTube, I have tried selling my sanity. I expected it to be able to see that the offending file is actually in the base path.
JoJoJohan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.