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?

4

2 回答 2

11

对于每个人来说,都在为同样的问题而战。只需'PIPELINE_ENABLED': True,从 django-pipeline 配置中删除即可。在此之后,django-pipeline 将在开发模式下为每个请求重新编译静态。

在部署之前,只需 runcollectstatic和 set DEBUG=False,如文档中所述。在此管道将停止为每个请求重新编译文件后,在模板中加载缩小的资产,您将能够STATIC_ROOT使用任何第三方代理服务器(如 NGINX)提供静态服务。

于 2016-05-31T09:27:18.303 回答
0

问题作者的原始答案很棒,但在我看来,您不需要手动执行此操作。您可以使用单个if else条件控件来使用此开关。

于 2018-05-14T00:38:36.563 回答