Reputation: 673
EDIT
- my_project
|_app
|_core
|_wsqi.py
|_settings
|_base.py <-- settings.py used for development
|_dev.py
|_prod.py
|_urls.py <-- modifications
|_requirements
|_base.txt
|_dev.txt
|_prod.txt
|_Dockerfile
|_Dockerfile.prod
|_entrypoint.sh
|_entrypoint.prod.sh
|_manage.py
|_.dockerignore
|_nginx
|_.env.dev
|_.env.prod
|_.gitignore
|_docker-compose.yml
|_docker-compose.prod.yml
# from django.conf import settings
import core
from django.conf.urls.static import static
urlpatterns = [
path('', views.home, name='home'),
path('registration/', include('django.contrib.auth.urls')),
path('randomization_management/', include('randomization_management.urls')),
path('randomization_settings/', include('randomization_settings.urls')),
path('randomization/', include('randomization.urls')),
path('export/', include('export.urls')),
path('contact/', views.contact, name='contact'),
path('admin/', admin.site.urls),
] + static(core.settings.dev.STATIC_URL, document_root=core.settings.dev.STATIC_ROOT)
I try to "dockerize" a Django apps.
All works fine except static files that are not served. I use Django web server (dev) so I should not have to "served" static files.
Neverthelsess, I run docker exec -it coverage_africa_web_1 python manage.py collectstatic
command
and get the confirmation
You have requested to collect static files at the destination location as specified in your settings:
/usr/src/app/static
This will overwrite existing files! Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes Found another file with the destination path 'randomization/js/script.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
140 static files copied to '/usr/src/app/static'.
settings.base.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
.env.dev
SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
restart: always
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
restart: always
volumes:
- postgres_data:/var/lib/postgres/data/
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
volumes:
postgres_data:
Upvotes: 0
Views: 341
Reputation: 673
OK, missing settings
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, "static") <- removed in dev
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), <- added in dev
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)
Upvotes: 0
Reputation: 351
I see that you don't use a web server to serve the static files, so make sure that Django is serving the static files.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
have a look to to Django documentation https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development
Upvotes: 0