Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Django 404 for web static files, but fine for admin static files

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.

like image 910
Dipak Avatar asked Jul 28 '16 04:07

Dipak


2 Answers

This was issue with the STATICFILES_DIRS configuration in the settings.py file.

This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

Following was the configuration in my settings.py:

STATIC_URL = '/static/'
STATIC_ROOT      =  os.path.join(BASE_DIR, "static") 

Now I updated this code to:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

And every files is loading fine.

Reference Link

like image 106
Dipak Avatar answered Oct 15 '22 04:10

Dipak


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:

whitenoise==3.3.1

2.Add whitenoise to your middleware apps inside settings.py

MIDDLEWARE_CLASSES = [# 'django.middleware.security.SecurityMiddleware','whitenoise.middleware.WhiteNoiseMiddleware',# ...]

make sure that you add this below your security.SecurityMiddleware app

3.Finally, change the following variables inside settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR,'<app_name>/static'),os.path.join(BASE_DIR, 'static'),)

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:

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
  1. Lastly disable the built-in django static file server as follows:

    INSTALLED_APPS = [
    # ...
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    # ...]
    
like image 28
ENDEESA Avatar answered Oct 15 '22 04:10

ENDEESA