I'm using django-compressor to compress my site's static CSS and Javascript files. Since I serve my site's static assets via Amazon S3, I'm also using django-storages to upload my files to S3.
我正在使用django-compressor来压缩我网站的静态CSS和Javascript文件。由于我通过Amazon S3提供我网站的静态资产,我还使用django-storage将我的文件上传到S3。
Here's my issue: I'm trying to make a clean base.html
template that all my site's other templates can inherit and extend. Here's what it looks like currently:
这是我的问题:我正在尝试创建一个干净的base.html模板,我的网站的所有其他模板都可以继承和扩展。这是目前的样子:
{% load compress %}
<html>
<head>
<!-- test -->
{% compress css %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ STATIC_URL }}css/styles.css" />
{% endcompress %}
{% compress css %}
{% block css %}{% endblock %}
{% endcompress %}
{% compress js %}
{% block js %}{% endblock %}
{% endcompress %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
As you can see, what I'm attempting to do here is allow my templates that inherit this template to override the css
and js
blocks, so they can define their own css and javascript to be compressed. Unfortunately, that's not what happens.
正如您所看到的,我在这里尝试做的是允许继承此模板的模板覆盖css和js块,以便他们可以定义自己的css和javascript进行压缩。不幸的是,事情并非如此。
When I run python manage.py compress
(to have django-compressor analyze my templates and generated the compressed javascript and css code), it doesn't actually find my included css and javascript files.
当我运行python manage.py compress(让django-compressor分析我的模板并生成压缩的javascript和css代码)时,它实际上并没有找到我包含的css和javascript文件。
For instance, here's my site's index.html
template:
例如,这是我网站的index.html模板:
{% block css %}
{{ block.super }}
<link rel="stylesheet" type="text/css" media="screen" href="{{ STATIC_URL }}css/index.css" />
{% endblock %}
When I attempt to visit that page on my site, I get an error saying that the compressed file doesn't exist.
当我尝试访问我的网站上的该页面时,我收到一条错误消息,指出压缩文件不存在。
I believe that what's happening is that the python manage.py compress
command isn't inspecting my templates that inherit from base.html
. And since it isn't analyzing them, it isn't generating any compressed code.
我相信发生的事情是python manage.py compress命令不检查从base.html继承的模板。由于它没有分析它们,因此它不会生成任何压缩代码。
I'd really like to get this working, because the only workaround I've found so far is to manually add {% compress %}...{% endcompress %}
tags in every single template file I have explicitly. I just hate to do that since it repeats so much code everywhere :(
我真的很想让这个工作,因为我到目前为止找到的唯一解决方法是在我明确指定的每个模板文件中手动添加{%compress%} ... {%endcompress%}标记。我只是讨厌这样做,因为它在所有地方都重复了这么多代码:(
Any advice would be greatly appreciated.
任何建议将不胜感激。
1 个解决方案
#1
5
I suppose you are using offline compression, in which case the template inheritance does not work as one would expect. See these issues relevant to this "problem":
我想您正在使用脱机压缩,在这种情况下,模板继承不能像预期的那样工作。查看与此“问题”相关的这些问题:
- https://github.com/jezdez/django_compressor/issues/171
- https://github.com/jezdez/django_compressor/issues/171
- https://github.com/jezdez/django_compressor/issues/175
- https://github.com/jezdez/django_compressor/issues/175
#1
5
I suppose you are using offline compression, in which case the template inheritance does not work as one would expect. See these issues relevant to this "problem":
我想您正在使用脱机压缩,在这种情况下,模板继承不能像预期的那样工作。查看与此“问题”相关的这些问题:
- https://github.com/jezdez/django_compressor/issues/171
- https://github.com/jezdez/django_compressor/issues/171
- https://github.com/jezdez/django_compressor/issues/175
- https://github.com/jezdez/django_compressor/issues/175