Django-压缩机抛出 UncompressableFileError
我正在使用 django-compressor 和 django-staticfiles (外部版本,我使用的是 Django 1.2)。
当我尝试加载网站时,出现错误:
TemplateSyntaxError: Caught UncompressableFileError while rendering: 'css/facebox.css' isn't accesible via COMPRESS_URL ('/static/') and can't be compressed
我已验证 COMPRESS_URL
等于 STATIC_URL
,并且实际上可以通过该 URL 访问该文件。
查看 django-compressor 代码,我发现抛出异常的位置:
def get_basename(self, url):
try:
base_url = self.storage.base_url
except AttributeError:
base_url = settings.COMPRESS_URL
# I added the following print statement:
print "url: %s, base_url: %s" % (url, base_url)
if not url.startswith(base_url):
raise UncompressableFileError("'%s' isn't accesible via "
"COMPRESS_URL ('%s') and can't be "
"compressed" % (url, base_url))
我的模板中的第一个 {% compress css %}
块是这样的:(
{% compress css %}
<link rel="stylesheet" href="/static/css/blueprint/screen.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/facebox.css" />
{% endcompress %}
请注意,第一个链接不使用 {{ STATIC_URL }}
,但第二个确实如此)
我在错误日志中看到了这一点:
[Thu Oct 13 08:19:13 2011] [error] url: /static/css/blueprint/screen.css, base_url: /static/
[Thu Oct 13 08:19:13 2011] [error] url: /static/css/facebox.css, base_url: /static/
[Thu Oct 13 08:19:14 2011] [error] url: /static/css/blueprint/screen.css, base_url: /static/
[Thu Oct 13 08:19:14 2011] [error] url: css/facebox.css, base_url: /static/
如您所见,screen.css 文件被处理两次,并且第二次成功。然而,facebox.css 文件第二次失败,可能是因为第二次解析文件时,模板上下文中未定义 {{ STATIC_URL }}
。
显然,我可以通过不使用 {{ STATIC_URL }}
来解决问题,但这不是一个可接受的解决方案。
为什么我的 css 文件会被处理两次?他们最初有 media='screen,projection'
,但我删除了它,认为它是导致问题的原因。
相关设置:
In [4]: from compressor.conf import settings
In [5]: settings.COMPRESS_ROOT
Out[5]: '/home/ianchat/static_files'
In [6]: settings.STATIC_ROOT
Out[6]: '/home/ianchat/static_files'
In [7]: settings.COMPRESS_URL
Out[7]: '/static/'
In [8]: settings.STATIC_URL
Out[8]: '/static/'
In [9]: settings.COMPRESS_OUTPUT_DIR
Out[9]: 'CACHE'
In [10]: settings.COMPRESS_CSS_FILTERS
Out[10]: ['compressor.filters.csstidy.CSSTidyFilter']
In [11]: settings.STATICFILES_FINDERS
Out[11]:
('staticfiles.finders.FileSystemFinder',
'staticfiles.finders.AppDirectoriesFinder',
'staticfiles.finders.LegacyAppDirectoriesFinder',
'compressor.finders.CompressorFinder')
I'm using django-compressor and django-staticfiles (the external version, I'm on Django 1.2).
When I try to load my site, I get an error:
TemplateSyntaxError: Caught UncompressableFileError while rendering: 'css/facebox.css' isn't accesible via COMPRESS_URL ('/static/') and can't be compressed
I've verified COMPRESS_URL
is equal to STATIC_URL
, and the file is actually accessible at that URL.
Looking at the django-compressor code, I found where that exception is thrown:
def get_basename(self, url):
try:
base_url = self.storage.base_url
except AttributeError:
base_url = settings.COMPRESS_URL
# I added the following print statement:
print "url: %s, base_url: %s" % (url, base_url)
if not url.startswith(base_url):
raise UncompressableFileError("'%s' isn't accesible via "
"COMPRESS_URL ('%s') and can't be "
"compressed" % (url, base_url))
The first {% compress css %}
block in my templates is this:
{% compress css %}
<link rel="stylesheet" href="/static/css/blueprint/screen.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/facebox.css" />
{% endcompress %}
(Note that the first link doesn't use {{ STATIC_URL }}
, but the second one does)
And I get this in my error log:
[Thu Oct 13 08:19:13 2011] [error] url: /static/css/blueprint/screen.css, base_url: /static/
[Thu Oct 13 08:19:13 2011] [error] url: /static/css/facebox.css, base_url: /static/
[Thu Oct 13 08:19:14 2011] [error] url: /static/css/blueprint/screen.css, base_url: /static/
[Thu Oct 13 08:19:14 2011] [error] url: css/facebox.css, base_url: /static/
As you can see, the screen.css file is processed twice, and successful the second time around. The facebox.css file, however, fails the second time, presumable because the {{ STATIC_URL }}
isn't defined in the template context the second time the file is parsed.
Obviously I could solve the problem by not using {{ STATIC_URL }}
, but that is not an acceptable solution.
Why would my css files be processed twice? They originally had media='screen, projection'
, but I removed that thinking it was causing the problem.
Relevant settings:
In [4]: from compressor.conf import settings
In [5]: settings.COMPRESS_ROOT
Out[5]: '/home/ianchat/static_files'
In [6]: settings.STATIC_ROOT
Out[6]: '/home/ianchat/static_files'
In [7]: settings.COMPRESS_URL
Out[7]: '/static/'
In [8]: settings.STATIC_URL
Out[8]: '/static/'
In [9]: settings.COMPRESS_OUTPUT_DIR
Out[9]: 'CACHE'
In [10]: settings.COMPRESS_CSS_FILTERS
Out[10]: ['compressor.filters.csstidy.CSSTidyFilter']
In [11]: settings.STATICFILES_FINDERS
Out[11]:
('staticfiles.finders.FileSystemFinder',
'staticfiles.finders.AppDirectoriesFinder',
'staticfiles.finders.LegacyAppDirectoriesFinder',
'compressor.finders.CompressorFinder')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题。
答案在这里找到:https://github.com/jezdez/django_compressor/pull/206
该链接的解决方案是执行 handler500。
我决定更改 500.html 模板以避免其中出现任何 {{STATIC_URL}},问题就解决了。
I bumped into the same issue.
The answer was found here: https://github.com/jezdez/django_compressor/pull/206
The link's solution is doing handler500.
I decided to change 500.html template to avoid any {{STATIC_URL}} in it and the problem was solved.
看起来 STATIC_URL 几乎不在您的上下文中。您确实配置了静态文件上下文处理器,对吧?您是否尝试过喜欢没有压缩器标签的文件?当您加载页面时,{{ STATIC_URL }} 是否正确显示在页面中?
我认为压缩器会检查 url,即使它通过文件系统访问它,查看 https://github.com/jezdez/django_compressor/blob/develop/compressor/base.py#L57
It almost looks like STATIC_URL is not in your context. You do have the staticfiles contextprocessor configured, right? Have you tried to like the file without the compressor tags? Does {{ STATIC_URL }} show up correctly in the page when you load it?
I think compressor checks the url even if it accesses it through the file system looking at https://github.com/jezdez/django_compressor/blob/develop/compressor/base.py#L57
这是一个老问题,但是搜索此错误消息时为数不多的搜索结果之一,因此可能值得分享我的解决方案。
就我而言,这是一个非常简单的情况:我对静态 url 进行了硬编码,并忘记了开头的
/
。所以我有这个:这给了我错误。更改为以下内容后:
已修复。当然,我后来意识到,我应该使用设置 STATIC_URL:
希望这对任何人都有帮助。
This is an old question, but one of the few search results when searching for this error message, so it might be worth someting to share my solution.
In my case it was a dead simple case: I hardcoded my static url and forgot the
/
at the beginning. So I had this:Which gave me the error. After changing to this:
It was fixed. Of course, I later realised, I should have used the setting STATIC_URL:
Hope this helps anyone.
我又看了一些,我很确定异常是由于尝试在没有第一遍的完整上下文的情况下显示未捕获的错误页面而引起的。这会导致 django-compressor 出现异常。[1]
当然,解决方案是处理所有错误。
[1] 我还运行一些非标准代码来显示静态页面,也许这会造成干扰,并且该错误不太常见。
I've look at it some more and I'm pretty sure the exception is caused by trying to display an uncatched error page without the full context the first pass had. This causes the exception in django-compressor.[1]
The solution, of course, is to handle all errors.
[1] I'm also running some non-standard code to display static pages, maybe this interferes and the reason that the bug is not too common.
我遇到了同样的问题;就我而言,问题是由使用
COMPRESS_OFFLINE_CONTEXT
引起的 - 它不会.update()
上下文,但它完全替换了它,从而删除了STATIC_URL
。我的解决方案只是将其添加回
COMPRESS_OFFLINE_CONTEXT
,在 local_settings 导入之后,否则任何覆盖都将不起作用。I ran into the same issue; in my case the problem was caused by using
COMPRESS_OFFLINE_CONTEXT
- which does not.update()
the context but it replaces it altogether, thus removingSTATIC_URL
.The solution in my case was just adding it back to the
COMPRESS_OFFLINE_CONTEXT
, after the local_settings import, otherwise any override there wouldn't have worked.