python - Django 1.8.2 不提供所有静态文件 - django-pipeline

标签 python django django-pipeline

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

为什么提供了网站图标,但没有提供压缩文件?我关注了official tutorial并仔细检查它。感谢您的帮助。

添加 该站点运行良好,通常提供静态文件。该问题只发生在来自 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 ###

[...]

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

最佳答案

static 文件夹位于何处?

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

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

在 django 1.8 中,我真的没有看到使用 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 正在寻找这些文件的位置。

如有疑问,look for more .

关于python - Django 1.8.2 不提供所有静态文件 - django-pipeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955183/

相关文章:

python - 如何在 Python 项目中正确组织文件

django - 如何在 Django 中正确测试 ModelForms 和 View

python - 连接 django 和 mysql 时出现 OperationalError

python - 运行collectstatic时Django-pipeline找不到文件或目录

python - 在 OpenCV-Python 中绘制直方图

python - Alembic - sqlalchemy 不检测现有表

python - 如何自动化 Python Dash 应用程序数据拉取?

提交时 Django 表单 BooleanField 返回 'on'

django - Yuglify 压缩器无法从通过 npm 安装的包中找到二进制文件

当 DEBUG=False 时 django 管道会中断管理