2

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)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
n2o
  • 6,175
  • 3
  • 28
  • 46
  • 1
    Thanks ! Could you add your settings related to STATIC files ? By the way, you should put your pipeline middleware on top of other middleware (the first in your MIDDLEWARE_CLASSES) because this middleware needs to be the latest to be run, in case of other middlewares also edit the HTML. Django runs the middleware from bottom to top. So in your case, django runs the MinifyHTMLMiddleware in first. – Pierre Criulanscy Jun 20 '15 at 17:28
  • I already pasted all settings for STATIC. Thanks for the info about Middleware. Changing position does not appear to change anything. I added the line from `urls.py` which may be interesting. Thanks! – n2o Jun 20 '15 at 17:41
  • Sorry I didn't scroll your pasted code...Ok so I think the error is in your urlpatterns about STATIC_URL. If you want to serve local file via the dev server you only need to add this rule : urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) – Pierre Criulanscy Jun 20 '15 at 22:31
  • Ok, removing the Staticfiles from `urls.py` did not change anything :-/ Still the same error... – n2o Jun 22 '15 at 12:17

1 Answers1

1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shu Hikari
  • 387
  • 4
  • 9
  • Thanks for your suggestion. I didn't consider using pdb in my Django projects until now... Maybe I'll try this. Already killed pipeline since it blocks the development and included the files manually. But I am using git, therefore I may switch to an old state and tell you how it worked. – n2o Aug 05 '15 at 10:53
  • Would be nice to hear if your problem is solved. Good luck :D – Shu Hikari Aug 05 '15 at 11:40
  • 1
    You asked where my static folder is located. The name of my Django app is `dpb` and it is stored in `/var/www/dpb/static` – n2o Aug 05 '15 at 17:32
  • @n2o Keep in mind that you have the BASE_DIR output as well, to find out the correct path. Did you made it work? Remember to disable pdb when you get it to work. – Shu Hikari Aug 05 '15 at 20:02
  • 1
    Thanks for your help. I am gonna test it on the weekend. Okay I will have a look at it – n2o Aug 06 '15 at 21:39