Django-compressor: 当 Debug 设置为 False 时,Django 不获取压缩文件

创建于 2012-12-22  ·  5评论  ·  资料来源: django-compressor/django-compressor

所以我让 django-compressor 使用 Debug = False 进行开发。 但是,一旦我设置了 Debug = True,django-compressor 似乎会生成正确的 URL、文件和所有内容,但 Django 本身会为这些文件返回 404。

我有

STATIC_ROOT = os.path.join(PROJECT_DIR, "static_media")

在我的项目的 static_media 文件夹中,我确实可以看到由压缩器创建的 CACHE/css/bfdeac875f7a.css 文件。

在生成的 HTML 文件中,我还可以看到

<link rel="stylesheet" href="/static/CACHE/css/bfdeac875f7a.css" type="text/css" />

但是当页面加载时,runserver 说:

[21/Dec/2012 22:28:44] "GET /static/CACHE/css/bfdeac875f7a.css HTTP/1.1" 404 276

我错过了什么吗? 我已经

  • 'django.core.context_processors.static',TEMPLATE_CONTEXT_PROCESSORS
  • 'compressor.finders.CompressorFinder',STATICFILES_FINDERS
  • 'compressor',INSTALLED_APPS

谢谢!

最有用的评论

抱歉,不敢相信我之前错过了:我以某种方式读到当 DEBUG 为 _True_ 时您遇到了问题。

反正。 当 DEBUG = False 时,默认情况下,由于性能和安全问题,您的 runserver 将拒绝提供静态文件本身。 这是为了防止您在生产中使用它。 为了能够在 DEBUG 为 False 时在本地正确测试您的静态文件,您需要将 --insecure 标志传递给 runserver,如此处所述: https ://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/

这能解决你的问题吗?

所有5条评论

你好,

我们需要更多关于您的设置的信息来帮助您:

  • 您的 COMPRESS_* 设置、STATIC_URL 设置、STATICFILES_* 设置
  • 您的 INSTALLED_APPS 中是否有静态文件?
  • 你的 Django 版本是什么?
  • 其他静态文件是否正常工作? 您正在使用 runserver 为他们服务,对吗?

谢谢

你好,

我正在使用 Django 1.4.3,具有以下设置:

...

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, "static_media")

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, "static"),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'coffee --compile --stdio'),
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)

...

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    ...
    'compressor',
    ...
)

...

我手动放入static_media也无法从/static/ url 访问,所以我一定没有正确配置静态文件......我也从 Django 1.3 迁移,如果这很重要。 你对我应该尝试什么有预感吗?

此外,当 Debug 为 true 时,runserver 成功获取我static_media文件夹中的文件,但当它为 false 时失败...

抱歉,不敢相信我之前错过了:我以某种方式读到当 DEBUG 为 _True_ 时您遇到了问题。

反正。 当 DEBUG = False 时,默认情况下,由于性能和安全问题,您的 runserver 将拒绝提供静态文件本身。 这是为了防止您在生产中使用它。 为了能够在 DEBUG 为 False 时在本地正确测试您的静态文件,您需要将 --insecure 标志传递给 runserver,如此处所述: https ://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/

这能解决你的问题吗?

哦,天哪,我的错。 我在最初的问题中无意中切换了“真”和“假”,抱歉造成混乱!

非常感谢,这确实解决了问题!

此页面是否有帮助?
0 / 5 - 0 等级