2

我正在运行 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)
4

1 回答 1

1

static你的文件夹在哪里?

如果它在 之外BASE_DIR,我的意思是,在 up 文件夹中,您可能必须在此处声明路径

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'templates/dpb'),
)

在 django 1.8 中,我真的没有看到 using 的必要性STATIC_ROOT,因为STATICFILES_DIRS它对于在每个地方统一静态目录非常有用。

考虑这个命令的输出os.path.join(BASE_DIR, 'templates/dpb')

找出问题所在的一个很好的解决方案是启动 pdb 调试器,以便像 Django 一样查看。

STATICFILES_DIRS在语句之后设置一个断点:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'templates/dpb'),
)
import pdb; pdb.set_trace()

像这样运行你的服务器:

python -m pdb manage.py runserver

这将在您的问题中停止 django 加载程序,从而可以轻松打印出 STATICFILES_DIR 并显示 Django 在哪里寻找这些文件。

如果有任何疑问,请寻找更多

于 2015-08-04T19:04:12.947 回答