django-compressor是否使用模板继承? [英] Does django-compressor work with template inheritance?

查看:238
本文介绍了django-compressor是否使用模板继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 django-compressor 压缩我网站的静态CSS和Javascript文件。由于我通过Amazon S3提供我的网站的静态资源,我还使用 django-storages

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.

这是我的问题:我试图创建一个干净的 base.html 模板所有我的网站的其他模板可以继承和扩展。以下是目前的样子:

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:

{% 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>

如您所见,我在尝试允许继承此模板的模板覆盖 css js 块,以便他们可以定义自己的css和javascript被压缩。不幸的是,这不是发生了什么。

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.

当我运行 python manage.py compress 我的模板和生成压缩的javascript和CSS代码),它实际上没有找到我包括的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.

例如,这里是我的网站的 index.html 模板:

For instance, here's my site's index.html template:

{% 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.

我相信发生的事情是 python manage.py compress 命令不检查继承自 base.html 的模板。因为它不是分析它们,它不生成任何压缩代码。

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.

我真的很想得到这个工作,因为唯一的解决方法,我发现到目前为止,我已经明确地在每个模板文件中手动添加 {%compress%} ... {%endcompress%} 标签。我只是讨厌这样做,因为它重复这么多的代码无处不在:(

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 :(

任何建议都将非常感激。

Any advice would be greatly appreciated.

推荐答案

我假设您使用的是离线压缩,在这种情况下,模板继承不会像预期的那样工作。查看与此问题相关的这些问题:

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/175

这篇关于django-compressor是否使用模板继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆