[Answer]-Django.staticfiles doesn't collect admin files

0πŸ‘

βœ…

Turns out there was a subtle problem with my settings.py splitting method. For anyone coming here from Google, I was following deploydjango.comβ€˜s and strategy on splitting settings.py, however ROOT_DIR was being defined in terms of the project, i.e. the following structure

$ tree -L 2
.
β”œβ”€β”€ static
β”œβ”€β”€ apps
└── project
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ settings
    β”‚Β Β  β”œβ”€β”€ __init__.py
    β”‚Β Β  β”œβ”€β”€ base.py
    β”‚Β Β  β”œβ”€β”€ dev.py
    β”‚Β Β  └── prod.py
    β”œβ”€β”€ urls.py
    └── wsgi.py

with the following setting

STATICFILES_DIRS = (
    ABS_PATH('apps', 'example_app', 'static'),
)

would result in ROOT_DIR being set to project/. And since ABS_PATH function defines paths based on ROOT_DIR, the apps/ folder is not visible (it should be preceded with '..').

The solution is of course to move apps/ folder inside the project/ folder, which makes sense. I.e. the correct structure is as follows:

$ tree -L 2
.
β”œβ”€β”€ static
└── project_name
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ apps                # <-- apps moved here
    β”‚Β Β  └── example_app
    β”œβ”€β”€ settings
    β”‚Β Β  β”œβ”€β”€ __init__.py
    β”‚Β Β  β”œβ”€β”€ base.py
    β”‚Β Β  β”œβ”€β”€ dev.py
    β”‚Β Β  └── prod.py
    β”œβ”€β”€ urls.py
    └── wsgi.py

I realised this problem is very tied to the way I was doing things, however since this structure can be seen by some people as β€œbest practice” (although some disagree), I hope this helps someone!

πŸ‘€gregoltsov

1πŸ‘

Check if you have all settings set like that in your settings.py.
I suppose that your static files are under static dir in your project root folder.

import os 
import sys

STATIC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

INSTALLED_APPS = (
# default apps
'django.contrib.staticfiles',
# etc
)
STATICFILES_DIRS = ()

nginx config:

 location /static {
        alias /path_to_your_project/static;
        access_log   off;
        expires      max;
 }
πŸ‘€nickzam

Leave a comment