简体   繁体   中英

Django collectstatics only collects admin static files

I have a django app running in docker containers with Nginx. All the static files (as css, js) for my app are being found successfully, with my nginx.conf file configured as it should be. But my django/admin static files are not being found.

I have found this solution , and tried as it says:

1) Set both STATIC_URL and STATIC_ROOT in your settings.py

2) Define just a single static entry in your nginx conf (with trailing slashes). No need for a second one that addresses static/admin/:

 location /static/ { alias /path/to/static/; } 

3) Use collectstatic which should collect admin -> static/admin. It will live under the same location as all the rest of your collected static media.

 python manage.py collectstatic 

When I do that, my STATIC_ROOT receives the admin static files, but is not receiving my app static files.

I have no problem with docker volumes. NGINX container is receiving STATIC_ROOT content. But I will share my .yml file because maybe you detect an error on it.

The "manage.py collectstatic" runs in my cldj/Dockerfile.

My files:

settings.py:

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

nginx.conf:

location /static/ {
    autoindex off;
    alias /static_files/;
}

docker-compose.yml:

version: '3'
volumes:
  static_files:

services:

  dj:
    build:
      context: .
      dockerfile: df/cldj/Dockerfile
    container_name: dj
    volumes:
      - ./proj:/proj
      - static_files:/proj/static_files
    ports:
      - 8000:8000
    command: gunicorn -w 4 proj.wsgi --bind 0.0.0.0:8000

  nx:
    build:
      context: .
      dockerfile: df/nx/Dockerfile
    container_name: nx
    volumes:
      - static_files:/static_files
    ports:
      - 80:80
      - 443:443
    depends_on:
      - dj

UPDATE

I have changed my settings.py as mentioned by Abhimanyu below:

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

With docker-compose build the app container is filled as wanted with collectstatic. But the NGINX container receives an empty folder called 'static_files'.

I realized that the docker-compose.yml contains the following lines in app service:

volumes:
  - ./proj:/proj
  - static_files:/proj/static_files

I changed it to point to 'static' folder (my STATIC_ROOT ):

volumes:
  - ./proj:/proj
  - static_files:/proj/static

But with this, the entire static/ folder is overwritten with the static/admin folder again (I loose my app staticfiles)

UPDATE 2 (Solution)

After following Abhimanyu instruction, I needed to remove old static_files volume using $docker volume rm static_files .

Now it's working fine.

use STATIC_ROOT = os.path.join(BASE_DIR, 'static/') and then run

python manage.py collectstatic

It will create a folder named static in your project directory. There you will find all your css and js files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM