Reputation: 6734
When running a Django project on my local Mac server I get 404 errors when trying to access static files.
My directory structure looks like:
myapp/static
In my settings.py I have tried a variety of combinations of the following:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, 'static'),
'/var/www/myapp/static',
'/pycharmprojects/myapp/static'
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
For every use of a static file I get an error like this in PyCharm:
GET /static/img/pastedsvg%2017.svg HTTP/1.1" 404 101
And an error like this in Firefox:
GET
http://127.0.0.1:8888/static/img/pastedsvg%2017.svg [HTTP/1.0 404 NOT FOUND 15ms]
I have looked at other answers to this problem in Stackoverflow and tried the suggestions but so far nothing works.
Upvotes: 1
Views: 1756
Reputation: 581
It is difficult to tell what exactly the issue might be, but here are some observations that may help:
STATIC_ROOT
is only used for collectstatic
. It is where collectstatic
will copy your static files when you run it from the command line (python -m django collectstatic
).STATIC_ROOT
should not be included in STATICFILES_DIRS
as this would confuse collectstatic
since its job is to copy static files from your static directories to STATIC_ROOT
.AppDirectoriesFinder
is going to look for static files in a directory called static
under each of your apps. So, for example, if your project is located in /pycharmprojects/myapp/
, AppDirectoriesFinder
will look for static files in directories such as /pycharmprojects/myapp/anapp/static
and /pycharmprojects/myapp/anotherapp/static
(for each application listed in INSTALLED_APPS
) and copy them to STATIC_ROOT
.FileSystemFinder
is going to use the values in STATICFILES_DIRS
to look for static files and copy them to STATIC_ROOT
(which is why your value for STATIC_ROOT
should not be listed in STATICFILES_DIRS
).collectstatic
as Django will look for files in their original location, but when not debug mode (DEBUG = False
in your Django config file), you will need to run collectstatic
in order for Django to find your static files.So, from this, I suggest:
os.path.join(PROJECT_ROOT, 'static')
from your STATICFILES_DIR
/pycharmprojects/myapp/static
from your STATICFILES_DIR
if that points to the same location as os.path.join(PROJECT_ROOT, 'static')
DEBUG = True
in your Django config file), run collectstatic
from the command line so that Django can see your static files.One other question. To which directory is your BASE_DIR
pointing? It seems like PROJECT_DIR
might be redundant since it's pointing to where BASE_DIR
usually points.
Hopefully something in this wall of text will be helpful.
Cheers!
Upvotes: 8
Reputation: 713
I don't know that the following is the correct answer, but I hope it will go well.
STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = [
# os.path.join(PROJECT_ROOT, "static"),
os.path.join(BASE_DIR, "static"),
]
print("PROJECT_ROOT", PROJECT_ROOT)
print("BASE_DIR", BASE_DIR)
In this case, the value of the variable named PROJECT_ROOT is the same directory that exists setting file. That is why it's not PROJECT_ROOT exactly.
You can execute when running server even if writing python-code in setting file. So you can verify that the code was written in properly once again.
Upvotes: 0