Django压缩器和模板继承 [英] Django-compressor and template inheritance

查看:139
本文介绍了Django压缩器和模板继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django 1.2.3中使用 django-compression 应用程序来缩小并合并一个数字包括CSS和JS文件。在基本模板中,我有

I'm using the django-compressor app in Django 1.2.3 to minify and merge a number of included CSS and JS files. In a base template, I have

{% load compress %}
{% compress js %}
{% block js %}
<script type="text/javascript" src="/site_media/js/jquery.query-2.1.7.js">
{% endblock %}

,而在小孩中,

{% block js %}
{{block.super}}
<script type="text/javascript" src="/site_media/js/jquery.validate.min.js">
{% endblock %}

当模板呈现时,第一个脚本标签被正确缩小,但第二个不是。在类似的情况下,我已经确认问题是继承。

When the templates render, the first script tag is correctly minified, but the second isn't. In similar scenarios, I've confirmed that the issue is inheritance.

我不想在子模板中继续使用compress标签,因为使用此应用程序的一点是合并文件并缩短HTTP请求。我错过了什么吗?还有另外一个解决方案吗?

I don't want to keep using compress tags in child templates, because half the point of using this app is to merge the files and and cut back on the HTTP requests. Am I missing something? Is there another solution I should look into?

推荐答案

我使用django-compression与Django 1.2,我把它设置成这样:

I use django-compressor with Django 1.2, and I set it up like this:

{% compress js %}
<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery-1.4.2.min.js"></script>
{% block extra_compressed_js %}{% endblock %}
{% endcompress %}

{% block external_js %}{% endblock %}

与我的 extra_compressed_js 块我经常会使用你描述的方法, {{block.super}} 通过继承添加更多的js。它对我来说没有任何麻烦。您必须注意的一件事是,所有要压缩的JS需要在本地文件系统上可用。这就是为什么我有一个单独的 external_js 块,对于来自外部源的JS。

And with my extra_compressed_js block I will often use the method you described, with {{ block.super }} to add more js through inheritance. It works for me without any trouble. One thing that you have to be careful about is that all the JS to compress needs to be available on the local filesystem. That's why I have a separate external_js block, for JS that comes from an outside source.

这听起来像我像其他事情正在发生。确保您的压缩机的副本是最新的,然后检查您的继承,以确保其实际正常工作。执行此操作的一种方法是在您的设置中设置 COMPRESS = False ,并确保所需的所有JavaScript实际上都显示在渲染的模板中。

It sounds to me like something else is going on. Make sure your copy of compressor is up to date, and then check on your inheritance to make sure it's actually working correctly. One way to do this is by setting COMPRESS=False in your settings and making sure that all of the javascript that you want included actually shows up in the rendered template.

这篇关于Django压缩器和模板继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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