首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django管道不加载资产

Django管道不加载资产
EN

Stack Overflow用户
提问于 2014-10-08 17:27:59
回答 2查看 1.5K关注 0票数 3

我跟随本教程制作了一个示例项目。这些文件的结构如下:

代码语言:javascript
复制
- 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-管道来管理资产。我已经将我的项目配置为与下面的代码相同的代码,但是它没有正确地加载资产。

settings.py

代码语言:javascript
复制
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

代码语言:javascript
复制
{% load compressed %}
{% compressed_css 'pollsX' %}

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

style.css

代码语言:javascript
复制
.red-me {
    color: red;
}

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

代码语言:javascript
复制
<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

EN

回答 2

Stack Overflow用户

发布于 2016-01-08 07:41:03

Django pipline更新非常频繁,所以您的特定教程已经过时了。但是我还是想回答你的问题,因为我只是花了几个小时用新的管道来解决同样的问题,并且想分享我的解决方案,希望它能对某人有所帮助。

每件事都适用于:

  • Django==1.9.1
  • django-管道=1.6.4

settings.py

代码语言:javascript
复制
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

代码语言:javascript
复制
{% load pipeline %}
{% stylesheet 'pollsX' %}
{% javascript 'pollsX' %}

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

Stack Overflow用户

发布于 2015-04-19 19:27:59

我想你把css文件名拼错了。而不是使用:

代码语言:javascript
复制
<link href="/static/styles1.css" rel="stylesheet" type="text/css" />

使用:

代码语言:javascript
复制
<link href="/static/style.css" rel="stylesheet" type="text/css" />
票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26262871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档