Django 1.8.2 not serving all staticfiles - django-pipeline

1.1k Views Asked by At

I am running Django 1.8.2 and I am using django-pipeline 1.5.1 to collect and compress my CSS and JS files.

After running python manage.py collectstatic, Django collects all files and compresses them as configured. But when I want to access the webserver, the dev-server does not serve all staticfiles. Namely the ones from django-pipeline cannot be loaded.

My template looks like this:

{% load pipeline %}
...
{% javascript 'master' %}

When the page is loaded in the dev-server, Django translate the code to:

<script charset="utf-8" src="/static/compressed/master.js" type="text/javascript"></script>

<link href="/static/img/favicon.ico" rel="icon"></link>

That's pretty good so far. But the files from pipeline cannot be served:

"GET /static/compressed/master.js HTTP/1.1" 404 1774

But I can see the failing master.js in my static-folder:

static
├── compressed
│   └── master.js
│   └── ...
└── img
    └── favicon.ico

Why is the favicon served, but the compressed files are not? I followed the official tutorial and double checked it. Thanks for you help.

Addition The site works well, staticfiles are normally served. The problem does only occur with the compressed files from django-pipeline.

Relevant settings

DEBUG = True

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    # 3rd party
    'pipeline',
    'filer',
    'mptt',
    'easy_thumbnails',
    'tinymce',

    # Own apps
    'polls',
    'pages',
    'login',
    'archive',
)   

MIDDLEWARE_CLASSES = (
    'pipeline.middleware.MinifyHTMLMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

[...]

STATIC_URL = '/static/'

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

# Define Paths for Pipeline
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'templates/dpb'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

### Pipeline ###

# Set Pipeline Compilers
PIPELINE_COMPILERS = (
  'pipeline.compilers.sass.SASSCompiler',
)

PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'

PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'

PIPELINE_ENABLED = True

PIPELINE_CSS = {
  'master': {
    'source_filenames': (
      'css/*.sass',
    ),
    'output_filename': 'compressed/master.css',
    'extra_context': {
      'media': 'screen, projection',
    },
  },
  'vendor': {
    'source_filenames': (
      'assets/bootstrap/css/bootstrap.min.css',
      'assets/bootstrap/css/bootstrap-theme.min.css',
      'assets/bootswatch/bootswatch.min.css',
    ),
    'output_filename': 'compressed/vendor.css'
  }
}

PIPELINE_JS = {
  'master': {
    'source_filenames': (
      'js/*.js',
    ),
    'output_filename': 'compressed/master.js'
  },
  'vendor': {
    'source_filenames': (
      'assets/jquery/jquery.min.js',
      'assets/bootstrap/js/bootstrap.min.js',
    ),
    'output_filename': 'compressed/vendor.js'
  }
}

### END Pipeline ###

[...]

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
1

There are 1 best solutions below

5
On

Where is you static folder located?

If it is outside the BASE_DIR, I mean, in the up folder, you may have to declare the path in here

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'templates/dpb'),
)

In django 1.8 I really didn't see the need for using STATIC_ROOT, since STATICFILES_DIRS are pretty useful for unite static dirs in everyplace.

Consider the output of the output of this command os.path.join(BASE_DIR, 'templates/dpb')

A good solution to find out what is wrong is to start pdb debugger in order to see as Django does.

Setup a break point right after STATICFILES_DIRS statement:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'templates/dpb'),
)
import pdb; pdb.set_trace()

Run your server like this:

python -m pdb manage.py runserver

This will stop the django loaders in right in your issue, making it easy to print out STATICFILES_DIR and showing where Django is looking for these files.

If any doubts, look for more.