让django项目与使用gzip文件的不同存储后端一起工作

时间:2015-04-14 10:42:44

标签: django gzip

我使用uwsgi作为应用程序服务器,使用nginx来提供静态文件。 使用支持gzip /缓存的其他STATICFILE_STORAGE时,设置失败,例如django.contrib.staticfiles.storage.ManifestStaticFilesStorage'whitenoise.django.GzipManifestStaticFilesStorage'

运行DEBUG = False时,我最终得到以下错误

Internal Server Error: /
Traceback (most recent call last):
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/response.py", line 103, in render
self.content = self.rendered_content
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/response.py", line 80, in rendered_content
content = template.render(context)
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 148, in render
return self._render(context)
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
return compiled_parent._render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
return compiled_parent._render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 898, in render
output = self.filter_expression.resolve(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 596, in resolve
obj = self.var.resolve(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 734, in resolve
value = self._resolve_lookup(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 788, in _resolve_lookup
current = current()

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 74, in super
return mark_safe(self.render(self.context))

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/templatetags/static.py", line 106, in render
url = self.url(context)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py", line 16, in url
return static(path)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py", line 9, in static
return staticfiles_storage.url(path)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 128, in url
hashed_name = self.stored_name(clean_name)

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 277, in stored_name
cache_name = self.clean_name(self.hashed_name(name))

File "/home/vagrant/envs/tuteria/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 91, in hashed_name
(clean_name, self))

ValueError: The file 'css/intlTelInput.css' could not be found with <django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 0xaae1f0c>.

我可以毫无问题地运行collectstatic命令。 以下是我的uwsgi配置

[uwsgi]
socket = /tmp/tuteria.sock
master=True
pidfile = /tmp/tuteria-master.pid
enable-threads=True
processes = 1
chdir = /home/vagrant/workspace/
module = config.wsgi
virtualenv = /home/vagrant/envs/tuteria/

我的设置文件如下

STATIC_ROOT = join(os.path.dirname(BASE_DIR), 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS
STATICFILES_DIRS = (
    join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
# STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'config.storages.GzipManifestPipelineStorage'
# 

NB 使用默认的STATICFILE_STORAGE

时,我不会遇到此问题

此处显示的错误消息表示找不到特定的静态文件。运行python manage.py collectstatic时通常会弹出此错误,但在这种情况下,我只在使用uwsgi运行服务器时出现此错误。 collectstatic获取所有静态文件并对它们进行gzips而没有任何问题。

1 个答案:

答案 0 :(得分:0)

我在django 1.7中遇到与ManifestStaticFilesStorage相同的问题。

在研究问题时我发现如果将DEBUG变量设置为True并运行命令collectstatic,则会收集静态文件。在进一步的命令中,collectstatic将正常使用DEBUG = False

UPD:你也可以停用你的collectstatic后端,运行collectstatic并再次激活另一个collectstatic后端并运行命令