6

I'm trying to compile static with django-pipeline, but can't manage to serve static in dev mode. Since I'm not a Django developer, I may be wrong how Django serves static files itself. Here's my project structure:

  • project (project itself)
    • project (settings, global urls config, etc.)
    • app (main and the only app)
      • static (app-based static files, namespaced via app name folder)
    • static (shared static, that's not linked to specific app)
      • css
        • app.styl
    • public
      • media
      • static (not used in dev env; only for production: should be served with nginx, auto-generated via collectstatic)

As I'm using shared static, I've specified STATICFILES_DIRS directive to allow Django dev server and collectstatic command to find shared static:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

Here's my settings.py:

# for production, should be served via nginx
STATIC_ROOT= os.path.join(BASE_DIR, 'public/static/')
# prefix for static app
STATIC_URL = '/static/'

# also django-pipeline config
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

PIPELINE = {
    'PIPELINE_ENABLED': True,
    'COMPILERS': (
        'pipeline.compilers.stylus.StylusCompiler',
    ),
    'STYLESHEETS': {
        'app': {
            'source_filenames': (
              'css/app.styl',
            ),
            'output_filename': 'css/app.css',
        },
    },
}

In my template I've specified CSS-group:

  {% load pipeline %}
  {% stylesheet 'app' %}
</head>

As a result, such HTML is generated:

  <link href="/static/css/app.css" rel="stylesheet" type="text/css" />
</head>

But /static/css/app.css returns 404.

If I run collectstatic, public/static/css/app.css is built. But as I understand Django logic, it's used only for apache/nginx-based production serving and not in dev one. In dev mode static is served via internal Django server with some middleware django-pipeline hooks.

What I'm doing wrong? Any help is appreciated, thanks.

UPD: I've added + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to global urlpatterns, however can't understand it's why it's recommended — STATIC_ROOT is used only for production with external proxy-server and not with Django itself. Or not?

1
  • If I set 'PIPELINE_ENABLED': False, Django is collecting static automatically everytime I hit HTTP-request in dev-mode. Is it OK?
    – f1nn
    May 29, 2016 at 15:33

2 Answers 2

11

For everyone, fighting with the same problem. Just remove 'PIPELINE_ENABLED': True, away from django-pipeline config. After this, django-pipeline will re-compile static for each request in dev mode.

And before deployment, just run collectstatic and set DEBUG=False, as it's mentioned in docs. After this pipeline will stop re-compiling files for each request, load minified assets in templates and you'll be able to serve static from STATIC_ROOT with any third-part proxy-server like NGINX.

1
  • 1
    FYI, the django-pipeline docs state that PIPELINE_ENABLED defaults to "not settings.DEBUG" so it really isn't necessary to set it as long as you're toggling DEBUG.
    – Jim
    Apr 26, 2020 at 3:48
0

Original answer of question's author is great, but it seems to me that you don't need to do that manually. You can have this switch with a single if else conditional control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.