Django管道不加载资产

时间:2014-10-08 17:28:00

标签: python django django-pipeline

我已跟随this tutorial制作示例项目。文件的结构是:

- mysite
    - mysite
         - __init__.py
         - settings.py
         - urls.py
         - wsgi.py
    - polls
         - migrations
         - templates
             - polls.html
         - static
             - script.js
             - style.css
         - admin.py
         - models.py
         - tests.py
         - urls.py
         - views.py
    - manage.py

一切运作良好,但问题是使用Django-pipeline来管理资产。我已将我的项目配置为与以下代码相同,但它没有正确加载资产。

settings.py

INSTALLED_APPS = (
    .
    .
    'django.contrib.staticfiles',
    'pipeline',
    'polls',
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
PIPELINE_ENABLED = True
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.cssmin.CSSMinCompressor'
PIPELINE_CSSMIN_BINARY = 'cssmin'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.slimit.SlimItCompressor'

PIPELINE_CSS = {
    'pollsX': {
        'source_filenames': (
          'style.css',
        ),
        'output_filename': 'styles1.css',
        'variant': 'datauri',
    },
}
PIPELINE_JS = {
    'pollsX': {
        'source_filenames': (
          'script.js',
        ),
        'output_filename': 'scripts1.js',
    }
}

polls.html

{% load compressed %}
{% compressed_css 'pollsX' %}

<div class='red-me'>
    <h1> Hi! I'm a templ! </h1>
</div>

的style.css

.red-me {
    color: red;
}

http://127.0.0.1/polls的生成输出是

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

<div class='red-me'>
    <h1> Hi! I'm a templ! </h1>
</div>

无法在浏览器中加载/static/styles1.css文件。甚至,我测试./manage.py collectstatic没有任何成功。我错过了什么吗?

Python-3.4和Django-1.7

2 个答案:

答案 0 :(得分:2)

Django pipline updates very frequently, so your particular tutorials are outdated already. But I want to answer on your question anyway, because I just spent couple hours in fixing the same issue with new pipeline, and want to share my solution and hope it will be helpful for someone.

Everything works for:

  • Django==1.9.1
  • django-pipeline==1.6.4

settings.py

INSTALLED_APPS = (
    .
    .
    'django.contrib.staticfiles',
    'pipeline',
    'polls',
)

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

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

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

PIPELINE = {
    'CSS_COMPRESSOR': 'pipeline.compressors.cssmin.CSSMinCompressor',
    'CSSMIN_BINARY': 'cssmin',
    'JS_COMPRESSOR': 'pipeline.compressors.slimit.SlimItCompressor',
    'STYLESHEETS': {
        'pollsX': {
            'source_filenames': (
                'style.css',
            ),
            'output_filename': 'css/styles1.css',
            'variant': 'datauri',
        },
    },
    'JAVASCRIPT': {
        'pollsX': {
            'source_filenames': (
                'script.js',
            ),
            'output_filename': 'js/scripts1.js',
        },
    }
}   

polls.html

{% load pipeline %}
{% stylesheet 'pollsX' %}
{% javascript 'pollsX' %}

<div class='red-me'>
    <h1> Hi! I'm a templ! </h1>
</div>

答案 1 :(得分:-3)

我认为你拼错了你的css文件名。 而不是使用:

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

使用:

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