我跟随本教程制作了一个示例项目。这些文件的结构如下:
- 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
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
发布于 2016-01-08 07:41:03
Django pipline更新非常频繁,所以您的特定教程已经过时了。但是我还是想回答你的问题,因为我只是花了几个小时用新的管道来解决同样的问题,并且想分享我的解决方案,希望它能对某人有所帮助。
每件事都适用于:
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>发布于 2015-04-19 19:27:59
我想你把css文件名拼错了。而不是使用:
<link href="/static/styles1.css" rel="stylesheet" type="text/css" />使用:
<link href="/static/style.css" rel="stylesheet" type="text/css" />https://stackoverflow.com/questions/26262871
复制相似问题