I have a Django (3.1) app that I've set up to serve static files, that is, myapp/settings.py has
DEBUG = True
.
.
.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
.
.
.
]
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = 'static/'
But instead of serving files from the static directory of myapp, it's serving static files from /Library/Python/3.7/site-packages/django/contrib/admin/static/. I have tested this by adding files to both directories,
~/Projects/myapp/static/file-from-project.js
/Library/Python/3.7/site-packages/django/contrib/admin/static/file-from-library.js
and then
python3 manage.py runserver
with a template including the lines
<script src="{% static 'file-from-project.js' %}"></script>
<script src="{% static 'file-from-library.js' %}"></script>
and file-from-library.js loads fine but I get a 404 for file-from-project.js. What am I doing wrong here?
Directory structure with the referenced files:
myapp
/myapp
settings.py
.
.
.
/static
file-from-project.js
.
.
.
/templates
.
.
.
manage.py
.
.
.
OK, I see the problem I think.
STATIC_ROOTis an absolute path, not a relative one. if you set it to eg.BASE_DIR / "static"you might get what you want.I usually set
BASE_DIRat the top of mysettings.pyas it gets used for a few things:The other thing is that you have
DEBUG=Truewhich means it's going to be looking at yourSTATIC_URLunder your app dirs. If you move the file to:It should work :)
Edit: I noticed
myappis an app, and not the project name, which makes me wonder what the project is called.Your static files for development for this structure should probably be in:
When you deploy, obviously
DEBUGshould beFalse, but here you should runcollectstaticin order to gather all the static files in one place for deployment.