Question

I noticed that staticfiles doesn't copy the admin's static files to STATIC_ROOT. I was under the impression (and I can't find references for that just now) that once you include django.contrib.staticfiles to your INSTALLED_APPS, it would automatically copy admin's static files (as well as all the other ones). However, it doesn't seem to be the case.

From browsing a dozen related questions on SO it seems that the accepted way is to include the hardcoded path to your virtualenv'd admin path to your NGINX, such as here:

location /static/admin {
  root   /webapps/hello_django/lib/python2.7/site-packages/django/contrib/admin/;
}

However, this seems rather dirty to me.

I should also mention that finders are working for me, i.e.

 $ ./manage.py findstatic admin
   Found 'admin' here:
   /<path to venv>/lib/python2.7/site-packages/django/contrib/admin/static/admin

Am I missing something here?

Was it helpful?

Solution 2

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!

OTHER TIPS

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;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top