Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-pipeline not serving files in development mode

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?

like image 857
f1nn Avatar asked May 29 '16 14:05

f1nn


1 Answers

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.

like image 154
f1nn Avatar answered Oct 23 '22 21:10

f1nn