please help me on this docker django configuration for serving static files.
my Django
project running on Docker
got some issues with delivering static files
.
All static files for admin view is loading fine, but static files for client web view is throwing 404 Not found Error.
This is my docker.yml
configuration details:
web:
build: ./web
expose:
- "8000"
links:
- postgres:postgres
volumes:
- ./web:/usr/src/app
ports:
- "8000:8000"
env_file: .env
command: python manage.py runserver 0.0.0.0:8000
postgres:
image: postgres:latest
volumes:
- /var/lib/postgresql
ports:
- "5432:5432"
update
This is the admin static file url will look like :
http://developer.com:8000/static/admin/css/base.css
and this is how client static file url looks like:
http://developer.com:8000/static/css/base.css
Where those admin folder in static directory is creator by running django command collectstatic
I have used this setting previously, and was working fine. But when I moved the project root folder to another directory seems have this issue.
I am totally stuck here, many many thanks for all your help and feedback.
This was issue with the
STATICFILES_DIRS
configuration in thesettings.py
file.Following was the configuration in my settings.py:
Now I updated this code to:
And every files is loading fine.
Reference Link
Use Whitenoise to make your life easier when dealing with static files in django.
1.If you are using docker-compose,add whitenoise to your requirements.txt file:
2.Add whitenoise to your middleware apps inside settings.py
make sure that you add this below your security.SecurityMiddleware app
3.Finally, change the following variables inside settings.py
Be sure to replace with the name of your app. Note that this only applies if your static files are stored in(for example) my_project/app/static/app/.
Otherwise if your static folder is located in my_project/app/static:
Lastly disable the built-in django static file server as follows:
As you have moved your project to another directory, there is a possibility that the path of your static directories are also different now.
Django in most scenarios use apache, nginx or some other web servers to serve static files
. One point to notice is that your static directory should be accessed publicly. I had gone through a problem like this before. What I did was Imoved static dir to document root mentioned in apache config file
.So move your static files to the doc root of apache and update static directories in settings.py to refer to the static directory in your apache doc root. I hope this helps.