我正在运行 Django 1.8.2,并且正在使用django-pipeline 1.5.1来收集和压缩我的 CSS 和 JS 文件。
运行后python manage.py collectstatic
,Django 收集所有文件并按照配置进行压缩。但是当我想访问网络服务器时,开发服务器并不提供所有静态文件。即无法加载来自django-pipeline的那些。
我的模板如下所示:
{% load pipeline %}
...
{% javascript 'master' %}
当页面在开发服务器中加载时,Django 将代码转换为:
<script charset="utf-8" src="/static/compressed/master.js" type="text/javascript"></script>
<link href="/static/img/favicon.ico" rel="icon"></link>
到目前为止还不错。但是无法提供管道中的文件:
"GET /static/compressed/master.js HTTP/1.1" 404 1774
master.js
但我可以在我的静态文件夹中看到失败:
static
├── compressed
│ └── master.js
│ └── ...
└── img
└── favicon.ico
为什么提供了网站图标,但没有提供压缩文件?我按照官方教程并仔细检查了它。谢谢你的帮助。
另外 该站点运行良好,通常提供静态文件。该问题仅发生在 django-pipeline 中的压缩文件中。
相关设置
DEBUG = True
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# 3rd party
'pipeline',
'filer',
'mptt',
'easy_thumbnails',
'tinymce',
# Own apps
'polls',
'pages',
'login',
'archive',
)
MIDDLEWARE_CLASSES = (
'pipeline.middleware.MinifyHTMLMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
[...]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Define Paths for Pipeline
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder',
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'templates/dpb'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
### Pipeline ###
# Set Pipeline Compilers
PIPELINE_COMPILERS = (
'pipeline.compilers.sass.SASSCompiler',
)
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_ENABLED = True
PIPELINE_CSS = {
'master': {
'source_filenames': (
'css/*.sass',
),
'output_filename': 'compressed/master.css',
'extra_context': {
'media': 'screen, projection',
},
},
'vendor': {
'source_filenames': (
'assets/bootstrap/css/bootstrap.min.css',
'assets/bootstrap/css/bootstrap-theme.min.css',
'assets/bootswatch/bootswatch.min.css',
),
'output_filename': 'compressed/vendor.css'
}
}
PIPELINE_JS = {
'master': {
'source_filenames': (
'js/*.js',
),
'output_filename': 'compressed/master.js'
},
'vendor': {
'source_filenames': (
'assets/jquery/jquery.min.js',
'assets/bootstrap/js/bootstrap.min.js',
),
'output_filename': 'compressed/vendor.js'
}
}
### END Pipeline ###
[...]
网址.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)